Free tools Windows power users keep installed

One-click scans. No signup required.

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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To make a line dashed in Matplotlib, pass linestyle="--" (or ls="--") to plot(). For a dash and gap length of your own choosing, pass dashes=[on, off, ...] instead, or call set_dashes() on a line that already exists. Every value is a length in points, and the list must alternate drawn and blank lengths, so it always contains an even number of entries.

Create a standard dashed line

The shortest route uses a built-in line style. The double-dash shorthand and the full name 'dashed' select the same default pattern.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(x, y, linestyle="--", label="Dashed")
ax.legend()
plt.show()

The pyplot format string also accepts --, as in ax.plot(x, y, "--r"), which combines the line style with a color. In shared code, the explicit linestyle keyword is easier to read, because a format string packs marker, line style, and color into one short token.

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

Set a custom dash and gap length

When the default dash is too long, too short, or too faint for your figure, give Matplotlib the exact sequence. The list reads as pairs of ink, space, and it repeats along the line. Two values give one dash length and one gap length. Four values give two alternating dash lengths, each followed by a gap.

line, = ax.plot(x, y, dashes=[6, 2])     # 6 pt dash, 2 pt gap
line.set_dashes([2, 2, 10, 2])          # short dash, gap, long dash, gap

The Matplotlib gallery’s dashed-line example uses the same sequences shown here, including [2, 2, 10, 2], [6, 2], and [4, 4], and it begins with the statement that the dashing of a line is controlled via a dash sequence.

Reading the pattern values

Sequence What it draws Typical use
[6, 2] 6 pt dash, 2 pt gap, repeating A clearly visible dash for a single secondary series
[4, 4] Equal 4 pt dash and gap An evenly spaced rule that reads as a grid or threshold
[2, 2, 10, 2] Short dash, gap, long dash, gap Distinguishing two dashed series that share one style

Lengths are in points, not data units, so the same sequence looks the same whether the x-axis spans one day or ten years. Scaling by line width is a separate effect covered in the defaults section below.

Shift where the pattern starts

A linestyle tuple has the form (offset, (on, off, ...)). The offset moves the starting point of the pattern along the line, measured in points. It matters when two dashed lines overlap, or when you want a dash to begin at the first data point rather than in the middle of a dash.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ax.plot(x, y, linestyle=(0, (5, 5)))   # pattern starts at the first point
ax.plot(x, y, linestyle=(2, (5, 5)))   # pattern begins 2 pt into the sequence

The tuple form and the dashes= keyword both describe the same underlying pattern. Use dashes= when you only need a sequence, and the tuple when the offset has to be set in the same argument.

Change an existing line

You do not need to recreate a line to change its dashing. Keep the object returned by plot() and call its setter.

  1. Capture the line: line, = ax.plot(x, y). The trailing comma unpacks the one-element list that plot() returns.
  2. Set the pattern: line.set_dashes([6, 2]).
  3. Redraw the figure. In an interactive session, call fig.canvas.draw_idle() if the change does not appear.

Calling set_dashes() replaces whatever linestyle the line had, so a line that was solid becomes dashed without a separate linestyle call.

Style the ends of dashes and color the gaps

Two options affect the finish rather than the rhythm of the pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Dash cap style controls how the ends of each dash look. The documented values are 'butt' (flat ends, the usual default), 'round' (semicircular ends that make short dashes look softer), and 'projecting' (ends extend past the nominal length, so short dashes look longer).
  • Gap color with gapcolor fills the spaces between dashes with a second color, which keeps two dashed series distinct when they run close together.
line, = ax.plot(x, y, dashes=[4, 4], gapcolor="tab:pink")
line.set_dash_capstyle("round")

# Equivalent keyword form at creation time:
ax.plot(x, y, dashes=[4, 4], dash_capstyle="round")

If gapcolor raises an error in your environment, check the installed Matplotlib version against the Line2D reference for your release, since the keyword’s availability depends on the version you run.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Make dashes the default across a project

Repeating the same keywords in every call is error-prone. Matplotlib exposes the default dash pattern for the -- style as the rcParam lines.dashed_pattern. Change it once and every line that uses linestyle="--" picks it up.

import matplotlib.pyplot as plt

plt.rcParams["lines.dashed_pattern"] = [6, 2]
ax.plot(x, y, linestyle="--")   # now uses 6 pt dash, 2 pt gap

For a set of settings that should travel between scripts, put the values in a style sheet file and apply it with a context manager. A file named dashed.mplstyle might contain the line lines.dashed_pattern: 6, 2, and the script applies it like this:

with plt.style.context("dashed.mplstyle"):
    ax.plot(x, y, linestyle="--")

Default patterns and how line width changes them

The stable Matplotlib documentation, labeled version 3.11.2 when checked in October 2026, lists these default patterns in points:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Style rcParam Default pattern
Dotted : lines.dotted_pattern [1.0, 1.65]
Dashed -- lines.dashed_pattern [3.7, 1.6]
Dash-dot -. lines.dashdot_pattern [6.4, 1.6, 1.0, 1.6]

These are defaults rather than fixed rules, and older or newer releases may differ, so check the values in your installed version with plt.rcParams["lines.dashed_pattern"] when exact spacing matters. Dash patterns are also scaled by line width by default. A line set to linewidth=3 therefore draws dashes three times longer than the same sequence at width 1. To keep the sequence unscaled, set plt.rcParams["lines.scale_dashes"] = False.

Troubleshoot common problems

  • The line looks solid. Confirm the call includes linestyle or dashes, and that a later call did not reset it. A linestyle="-" argument after dashes= can overwrite the dash pattern on some code paths, so keep one source of truth per line.
  • The dashes are too big or too small. The line width is probably scaling them. Adjust the sequence, or turn off lines.scale_dashes as shown above.
  • The dash pattern has an odd number of values. Dash sequences need an even count of on and off lengths. Add a gap value, or repeat the pair.
  • A change to rcParams has no effect. Set the rcParam before the line is created. Existing lines keep the pattern they were drawn with unless you call set_dashes() on them.

Which method to choose

  • Use linestyle="--" when the default dash is acceptable and the code should stay short.
  • Use dashes=[...] when you need a specific length and gap at creation time.
  • Use the tuple form when the starting phase of the pattern matters.
  • Use set_dashes() or set_dash_capstyle() when a line already exists and should change in place.
  • Use lines.dashed_pattern or a style sheet when the same look should apply across an entire script or project.

The source material for these examples is Matplotlib’s official documentation, including its dashed-line example gallery, its linestyle reference, the Line2D and pyplot.plot API pages, and its customization tutorial. Those pages are updated with each release, so confirm the defaults against the version you have installed.

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.