Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Create Dashed Contour Lines in Python Matplotlib

Use Matplotlib’s linestyles argument to dash every contour line, customize the pattern, or distinguish negative contours from the rest.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

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.Support on Ko-Fi

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Troubleshoot missing or unexpected contours

  • Only negative contours are dashed: this may be the monochrome negative-contour convention. Set linestyles explicitly to style all levels, or use the negative-contour setting when only negative levels should differ.
  • No lines appear: check that Z has the expected shape for X and Y, and that the requested contour levels fall within the values in Z.
  • You are using contourf: add a separate contour call 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 linestyles when 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.