|
19 | 19 | from collections import OrderedDict
|
20 | 20 | from matplotlib.transforms import blended_transform_factory
|
21 | 21 |
|
22 |
| -linestyles = [ |
| 22 | + |
| 23 | +linestyle_str = [ |
23 | 24 | ('solid', ('solid')), # Same as (0, ()) or '-'
|
24 | 25 | ('dotted', ('dotted')), # Same as (0, (1, 1)) or '.'
|
25 | 26 | ('dashed', ('dashed')), # Same as '--'
|
26 |
| - ('dashdot', ('dashdot')), # Same as '-.' |
| 27 | + ('dashdot', ('dashdot'))] # Same as '-.' |
27 | 28 |
|
| 29 | +linestyle_tuple = [ |
28 | 30 | ('loosely dotted', (0, (1, 10))),
|
29 | 31 | ('densely dotted', (0, (1, 1))),
|
30 | 32 |
|
|
38 | 40 | ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
|
39 | 41 | ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]
|
40 | 42 |
|
41 |
| -# Reverse the list so that plots and linestyles are in same order. |
42 |
| -linestyles = OrderedDict(linestyles[::-1]) |
43 |
| -X, Y = np.linspace(0, 100, 10), np.zeros(10) |
44 | 43 |
|
45 |
| -fig, ax = plt.subplots(figsize=(10, 6)) |
46 |
| -for i, (name, linestyle) in enumerate(linestyles.items()): |
47 |
| - ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') |
| 44 | +def simple_plot(ax, linestyles): |
| 45 | + yticklabels = [] |
| 46 | + for i, (name, linestyle) in enumerate(linestyles): |
| 47 | + ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black') |
| 48 | + yticklabels.append(name) |
| 49 | + |
| 50 | + ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5), |
| 51 | + yticks=np.arange(len(linestyles)), yticklabels=yticklabels) |
48 | 52 |
|
49 |
| -ax.set(xticks=[], ylim=(-0.5, len(linestyles)-0.5), |
50 |
| - yticks=np.arange(len(linestyles)), yticklabels=linestyles.keys()) |
| 53 | + # For each line style, add a text annotation with a small offset from |
| 54 | + # the reference point (0 in Axes coords, y tick value in Data coords). |
| 55 | + reference_transform = blended_transform_factory(ax.transAxes, ax.transData) |
| 56 | + for i, (name, linestyle) in enumerate(linestyles): |
| 57 | + ax.annotate(repr(linestyle), xy=(0.0, i), xycoords=reference_transform, |
| 58 | + xytext=(-6, -12), textcoords='offset points', color="blue", |
| 59 | + fontsize=8, ha="right", family="monospace") |
| 60 | + |
| 61 | + |
| 62 | +X, Y = np.linspace(0, 100, 10), np.zeros(10) |
| 63 | +fig, ax = plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 2]}, |
| 64 | + figsize=(10, 6)) |
51 | 65 |
|
52 |
| -# For each line style, add a text annotation with a small offset from |
53 |
| -# the reference point (0 in Axes coords, y tick value in Data coords). |
54 |
| -reference_transform = blended_transform_factory(ax.transAxes, ax.transData) |
55 |
| -for i, (name, linestyle) in enumerate(linestyles.items()): |
56 |
| - ax.annotate(repr(linestyle), xy=(0.0, i), xycoords=reference_transform, |
57 |
| - xytext=(-6, -12), textcoords='offset points', color="blue", |
58 |
| - fontsize=8, ha="right", family="monospace") |
| 66 | +simple_plot(ax[0], linestyle_str[::-1]) |
| 67 | +simple_plot(ax[1], linestyle_tuple[::-1]) |
59 | 68 |
|
60 | 69 | plt.tight_layout()
|
61 | 70 | plt.show()
|
0 commit comments