Skip to content

Dashed Lines

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 500)
y = np.sin(x)

plt.rc("lines", linewidth=2.5)
fig, ax = plt.subplots()

# Using set_dashes() and set_capstyle() to modify dashing of an existing line.
(line1,) = ax.plot(x, y, label="Using set_dashes() and set_dash_capstyle()")
line1.set_dashes([2, 2, 10, 2])  # 2pt line, 2pt break, 10pt line, 2pt break.
line1.set_dash_capstyle("round")

# Using plot(..., dashes=...) to set the dashing when creating a line.
(line2,) = ax.plot(x, y - 0.2, dashes=[6, 2], label="Using the dashes parameter")

# Using plot(..., dashes=..., gapcolor=...) to set the dashing and
# alternating color when creating a line.
(line3,) = ax.plot(x, y - 0.4, dashes=[4, 4], gapcolor="tab:pink", label="Using the dashes and gapcolor parameters")

ax.legend(handlelength=4)
plt.show()

Dashed Lines - dark

Dashed Lines - light

Note

The images above are generated by the test suite (tests/test_style_images.py), which runs every snippet in docs/snippets/ under both styles.