|
9 | 9 | import matplotlib.pyplot as plt |
10 | 10 | import numpy as np |
11 | 11 |
|
12 | | -prop_cycle = plt.rcParams['axes.prop_cycle'] |
13 | | -colors = prop_cycle.by_key()['color'] |
| 12 | +from matplotlib.colors import TABLEAU_COLORS, same_color |
| 13 | + |
14 | 14 |
|
15 | | -lwbase = plt.rcParams['lines.linewidth'] |
16 | | -thin = lwbase / 2 |
17 | | -thick = lwbase * 3 |
| 15 | +def f(x, a): |
| 16 | + """A nice sigmoid-like parametrized curve, ending approximately at *a*.""" |
| 17 | + return 0.85 * a * (1 / (1 + np.exp(-x)) + 0.2) |
18 | 18 |
|
19 | | -fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True) |
20 | | -for icol in range(2): |
21 | | - if icol == 0: |
22 | | - lwx, lwy = thin, lwbase |
23 | | - else: |
24 | | - lwx, lwy = lwbase, thick |
25 | | - for irow in range(2): |
26 | | - for i, color in enumerate(colors): |
27 | | - axs[irow, icol].axhline(i, color=color, lw=lwx) |
28 | | - axs[irow, icol].axvline(i, color=color, lw=lwy) |
29 | 19 |
|
30 | | - axs[1, icol].set_facecolor('k') |
31 | | - axs[1, icol].xaxis.set_ticks(np.arange(0, 10, 2)) |
32 | | - axs[0, icol].set_title(f'line widths (pts): {lwx:g}, {lwy:g}', |
33 | | - fontsize='medium') |
| 20 | +fig, ax = plt.subplots() |
| 21 | +ax.axis('off') |
| 22 | +ax.set_title("Colors in the default property cycle") |
34 | 23 |
|
35 | | -for irow in range(2): |
36 | | - axs[irow, 0].yaxis.set_ticks(np.arange(0, 10, 2)) |
| 24 | +prop_cycle = plt.rcParams['axes.prop_cycle'] |
| 25 | +colors = prop_cycle.by_key()['color'] |
| 26 | +x = np.linspace(-4, 4, 200) |
37 | 27 |
|
38 | | -fig.suptitle('Colors in the default prop_cycle', fontsize='large') |
| 28 | +for i, (color, color_name) in enumerate(zip(colors, TABLEAU_COLORS)): |
| 29 | + assert same_color(color, color_name) |
| 30 | + pos = 4.5 - i |
| 31 | + ax.plot(x, f(x, pos)) |
| 32 | + ax.text(4.2, pos, f"'C{i}': '{color_name}'", color=color, va="center") |
| 33 | + ax.bar(9, 1, width=1.5, bottom=pos-0.5) |
39 | 34 |
|
40 | 35 | plt.show() |
41 | 36 |
|
|
46 | 41 | # The use of the following functions, methods, classes and modules is shown |
47 | 42 | # in this example: |
48 | 43 | # |
49 | | -# - `matplotlib.axes.Axes.axhline` / `matplotlib.pyplot.axhline` |
50 | | -# - `matplotlib.axes.Axes.axvline` / `matplotlib.pyplot.axvline` |
51 | | -# - `matplotlib.axes.Axes.set_facecolor` |
52 | | -# - `matplotlib.figure.Figure.suptitle` |
| 44 | +# - `matplotlib.axes.Axes.axis` |
| 45 | +# - `matplotlib.axes.Axes.text` |
| 46 | +# - `matplotlib.colors.same_color` |
| 47 | +# - `cycler.Cycler` |
53 | 48 | # |
54 | 49 | # .. tags:: |
55 | 50 | # |
56 | 51 | # styling: color |
57 | | -# styling: colormap |
| 52 | +# purpose: reference |
58 | 53 | # plot-type: line |
59 | 54 | # level: beginner |
0 commit comments