|
3 | 3 | Linestyles
|
4 | 4 | ==========
|
5 | 5 |
|
6 |
| -This examples showcases different linestyles copying those of Tikz/PGF. |
| 6 | +Simple linestyles can be defined using the strings "solid", "dotted", "dashed" |
| 7 | +or "dashdot". More refined control can be achieved by providing a dash tuple |
| 8 | +``(offset, (on_off_seq))``. For example, ``(0, (3, 10, 1, 15))`` means |
| 9 | +(3pt line, 10pt space, 1pt line, 15pt space) with no offset. See also |
| 10 | +`.Line2D.set_linestyle`. |
| 11 | +
|
| 12 | +*Note*: The dash style can also be configured via `.Line2D.set_dashes` |
| 13 | +as shown in :doc:`/gallery/lines_bars_and_markers/line_demo_dash_control` |
| 14 | +and passing a list of dash sequences using the keyword *dashes* to the |
| 15 | +cycler in :doc:`property_cycle </tutorials/intermediate/color_cycle>`. |
7 | 16 | """
|
8 | 17 | import numpy as np
|
9 | 18 | import matplotlib.pyplot as plt
|
10 |
| -from collections import OrderedDict |
11 | 19 | from matplotlib.transforms import blended_transform_factory
|
12 | 20 |
|
13 |
| -linestyles = OrderedDict( |
14 |
| - [('solid', (0, ())), |
15 |
| - ('loosely dotted', (0, (1, 10))), |
16 |
| - ('dotted', (0, (1, 5))), |
17 |
| - ('densely dotted', (0, (1, 1))), |
| 21 | +linestyle_str = [ |
| 22 | + ('solid', 'solid'), # Same as (0, ()) or '-' |
| 23 | + ('dotted', 'dotted'), # Same as (0, (1, 1)) or '.' |
| 24 | + ('dashed', 'dashed'), # Same as '--' |
| 25 | + ('dashdot', 'dashdot')] # Same as '-.' |
18 | 26 |
|
19 |
| - ('loosely dashed', (0, (5, 10))), |
20 |
| - ('dashed', (0, (5, 5))), |
21 |
| - ('densely dashed', (0, (5, 1))), |
| 27 | +linestyle_tuple = [ |
| 28 | + ('loosely dotted', (0, (1, 10))), |
| 29 | + ('dotted', (0, (1, 1))), |
| 30 | + ('densely dotted', (0, (1, 1))), |
22 | 31 |
|
23 |
| - ('loosely dashdotted', (0, (3, 10, 1, 10))), |
24 |
| - ('dashdotted', (0, (3, 5, 1, 5))), |
25 |
| - ('densely dashdotted', (0, (3, 1, 1, 1))), |
| 32 | + ('loosely dashed', (0, (5, 10))), |
| 33 | + ('dashed', (0, (5, 5))), |
| 34 | + ('densely dashed', (0, (5, 1))), |
| 35 | + |
| 36 | + ('loosely dashdotted', (0, (3, 10, 1, 10))), |
| 37 | + ('dashdotted', (0, (3, 5, 1, 5))), |
| 38 | + ('densely dashdotted', (0, (3, 1, 1, 1))), |
26 | 39 |
|
27 |
| - ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), |
28 | 40 | ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
|
29 |
| - ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]) |
| 41 | + ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), |
| 42 | + ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))] |
| 43 | + |
| 44 | + |
| 45 | +def plot_linestyles(ax, linestyles): |
| 46 | + X, Y = np.linspace(0, 100, 10), np.zeros(10) |
| 47 | + yticklabels = [] |
| 48 | + |
| 49 | + for i, (name, linestyle) in enumerate(linestyles): |
| 50 | + ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') |
| 51 | + yticklabels.append(name) |
30 | 52 |
|
| 53 | + ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5), |
| 54 | + yticks=np.arange(len(linestyles)), yticklabels=yticklabels) |
31 | 55 |
|
32 |
| -plt.figure(figsize=(10, 6)) |
33 |
| -ax = plt.subplot(1, 1, 1) |
| 56 | + # For each line style, add a text annotation with a small offset from |
| 57 | + # the reference point (0 in Axes coords, y tick value in Data coords). |
| 58 | + reference_transform = blended_transform_factory(ax.transAxes, ax.transData) |
| 59 | + for i, (name, linestyle) in enumerate(linestyles): |
| 60 | + ax.annotate(repr(linestyle), xy=(0.0, i), xycoords=reference_transform, |
| 61 | + xytext=(-6, -12), textcoords='offset points', color="blue", |
| 62 | + fontsize=8, ha="right", family="monospace") |
34 | 63 |
|
35 |
| -X, Y = np.linspace(0, 100, 10), np.zeros(10) |
36 |
| -for i, (name, linestyle) in enumerate(linestyles.items()): |
37 |
| - ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') |
38 | 64 |
|
39 |
| -ax.set_ylim(-0.5, len(linestyles)-0.5) |
40 |
| -plt.yticks(np.arange(len(linestyles)), linestyles.keys()) |
41 |
| -plt.xticks([]) |
| 65 | +fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 3]}, |
| 66 | + figsize=(10, 8)) |
42 | 67 |
|
43 |
| -# For each line style, add a text annotation with a small offset from |
44 |
| -# the reference point (0 in Axes coords, y tick value in Data coords). |
45 |
| -reference_transform = blended_transform_factory(ax.transAxes, ax.transData) |
46 |
| -for i, (name, linestyle) in enumerate(linestyles.items()): |
47 |
| - ax.annotate(str(linestyle), xy=(0.0, i), xycoords=reference_transform, |
48 |
| - xytext=(-6, -12), textcoords='offset points', color="blue", |
49 |
| - fontsize=8, ha="right", family="monospace") |
| 68 | +plot_linestyles(ax0, linestyle_str[::-1]) |
| 69 | +plot_linestyles(ax1, linestyle_tuple[::-1]) |
50 | 70 |
|
51 | 71 | plt.tight_layout()
|
52 | 72 | plt.show()
|
0 commit comments