Set linestyles="dashed" in a contour() call to make every contour line dashed. Use contour for line contours; contourf fills the regions between levels.
Make every contour line dashed
Pass the linestyles argument to either the object-oriented Axes.contour method or the pyplot plt.contour function. This example creates a grid, plots nine levels, labels them, and displays the figure:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 121)
y = np.linspace(-2, 2, 81)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
fig, ax = plt.subplots()
levels = np.linspace(-1, 1, 9)
cs = ax.contour(X, Y, Z, levels=levels, linestyles="dashed")
ax.clabel(cs)
plt.show()
To use pyplot instead, replace the contour call with cs = plt.contour(X, Y, Z, levels=levels, linestyles="dashed"); the plt.contour interface accepts the same style argument. See the Matplotlib contour API documentation (stable documentation identifies version 3.11.2; check your installed version if behavior differs).
Choose a dash pattern
For a standard dashed line, use "dashed" or its short form "--". Matplotlib also documents "solid" (or "-"), "dotted" (or ":"), and "dashdot" (or "-."). A tuple lets you set a custom pattern; for example, (0, (5, 5)) means a zero offset followed by five points drawn and five skipped:
#1 Best Overall
cs = ax.contour(X, Y, Z, levels=levels, linestyles=(0, (5, 5)))
For a different style at each level, pass a sequence of styles in the same order as the contour levels. If all levels should look alike, pass one string or tuple. Pattern units are points, so dash spacing can look different as line width, figure size, or rendered output changes; preview the figure at its intended export size. See Matplotlib’s line-style documentation.
Distinguish negative contours from all contours
In the Matplotlib contour gallery’s monochrome example, negative contour levels are dashed by default. That convention is different from explicitly setting every contour to dashed: use linestyles="dashed" on the call when you want one uniform style across the contour set. The gallery shows how to make negative contours solid instead:
Rank #2
plt.rcParams["contour.negative_linestyle"] = "solid"
For negative-only styling, Matplotlib’s contour API also provides negative-line-style control. Check the behavior against the Matplotlib version installed in your environment. The documented convention is described in the contour gallery.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Use line contours, not filled contours, for dashed curves
contour() draws contour lines; contourf() fills the intervals between levels. If a plot needs filled regions with dashed boundaries, overlay a line-contour call on top of the filled contour:
ax.contourf(X, Y, Z, levels=levels)
ax.contour(X, Y, Z, levels=levels, linestyles="dashed")
The line-contour call supplies the dashed edges. The contour API documentation describes the line-contour interface and points to it for contour edges.
Quick Recap
Best Value
Troubleshoot missing or unexpected contours
- Only negative contours are dashed: this may be the monochrome negative-contour convention. Set
linestylesexplicitly to style all levels, or use the negative-contour setting when only negative levels should differ. - No lines appear: check that
Zhas the expected shape forXandY, and that the requested contour levels fall within the values inZ. - You are using
contourf: add a separatecontourcall to draw dashed boundaries. - Dashes look too dense or sparse: adjust the custom dash tuple and line width, then inspect the output at its final dimensions.
- Older code mutates contour collections: set
linestyleswhen creating the contour set rather than relying on per-collection mutation patterns that can differ across releases.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




