|
1 | 1 | """
|
2 |
| -======================================= |
3 |
| -A simple plot with a custom dashed line |
4 |
| -======================================= |
| 2 | +============================== |
| 3 | +Customizing dashed line styles |
| 4 | +============================== |
5 | 5 |
|
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. |
8 | 20 | """
|
9 | 21 | import numpy as np
|
10 | 22 | import matplotlib.pyplot as plt
|
11 | 23 |
|
12 |
| - |
13 | 24 | 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) |
15 | 26 |
|
16 | 27 | fig, ax = plt.subplots()
|
17 |
| -line1, = ax.plot(x, np.sin(x), '--', linewidth=2, |
18 |
| - label='Dashes set retroactively') |
19 |
| -line1.set_dashes(dashes) |
20 | 28 |
|
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') |
23 | 35 |
|
24 |
| -ax.legend(loc='lower right') |
| 36 | +ax.legend() |
25 | 37 | plt.show()
|
0 commit comments