Skip to content

Commit 98bfb96

Browse files
authored
Merge pull request #10992 from timhoffm/example-dashes
Rewrite dashes example
2 parents 02a0f8b + f3d1929 commit 98bfb96

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
"""
2-
=======================================
3-
A simple plot with a custom dashed line
4-
=======================================
2+
==============================
3+
Customizing dashed line styles
4+
==============================
55
6-
A Line object's ``set_dashes`` method allows you to specify dashes with
7-
a series of on/off lengths (in points).
6+
The dashing of a line is controlled via a dash sequence. It can be modified
7+
using `.Line2D.set_dashes`.
8+
9+
The dash sequence is a series of on/off lengths in points, e.g.
10+
``[3, 1]`` would be 3pt long lines separated by 1pt spaces.
11+
12+
Some functions like `.Axes.plot` support passing Line properties as keyword
13+
arguments. In such a case, you can already set the dashing when creating the
14+
line.
15+
16+
*Note*: The dash style can also be configured via a
17+
:ref:`property_cycle <sphx_glr_tutorials_intermediate_color_cycle.py>`
18+
by passing a list of dash sequences using the keyword *dashes* to the
19+
cycler. This is not shown within this example.
820
"""
921
import numpy as np
1022
import matplotlib.pyplot as plt
1123

12-
1324
x = np.linspace(0, 10, 500)
14-
dashes = [10, 5, 100, 5] # 10 points on, 5 off, 100 on, 5 off
25+
y = np.sin(x)
1526

1627
fig, ax = plt.subplots()
17-
line1, = ax.plot(x, np.sin(x), '--', linewidth=2,
18-
label='Dashes set retroactively')
19-
line1.set_dashes(dashes)
2028

21-
line2, = ax.plot(x, -1 * np.sin(x), dashes=[30, 5, 10, 5],
22-
label='Dashes set proactively')
29+
# Using set_dashes() to modify dashing of an existing line
30+
line1, = ax.plot(x, y, label='Using set_dashes()')
31+
line1.set_dashes([2, 2, 10, 2]) # 2pt line, 2pt break, 10pt line, 2pt break
32+
33+
# Using plot(..., dashes=...) to set the dashing when creating a line
34+
line2, = ax.plot(x, y - 0.2, dashes=[6, 2], label='Using the dashes parameter')
2335

24-
ax.legend(loc='lower right')
36+
ax.legend()
2537
plt.show()

0 commit comments

Comments
 (0)