|
| 1 | +""" |
| 2 | +===================== |
| 3 | +Named Color Sequences |
| 4 | +===================== |
| 5 | +
|
| 6 | +Matplotlib's `~matplotlib.colors.ColorSequenceRegistry` allows access to |
| 7 | +predefined lists of colors by name e.g. |
| 8 | +``colors = matplotlib.color_sequences['Set1']``. This example shows all of the |
| 9 | +built in color sequences. |
| 10 | +
|
| 11 | +Note that user-defined sequences can be added via the registry's |
| 12 | +`~matplotlib.colors.ColorSequenceRegistry.register` method. |
| 13 | +""" |
| 14 | + |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import numpy as np |
| 17 | + |
| 18 | +import matplotlib as mpl |
| 19 | + |
| 20 | + |
| 21 | +def plot_color_sequences(names, ax): |
| 22 | + # Display each named color sequence horizontally on the supplied axes. |
| 23 | + |
| 24 | + for n, name in enumerate(names): |
| 25 | + colors = mpl.color_sequences[name] |
| 26 | + n_colors = len(colors) |
| 27 | + x = np.arange(n_colors) |
| 28 | + y = np.full_like(x, n) |
| 29 | + |
| 30 | + ax.scatter(x, y, facecolor=colors, edgecolor='dimgray', s=200, zorder=2) |
| 31 | + |
| 32 | + ax.set_yticks(range(len(names)), labels=names) |
| 33 | + ax.grid(visible=True, axis='y') |
| 34 | + ax.yaxis.set_inverted(True) |
| 35 | + ax.xaxis.set_visible(False) |
| 36 | + ax.spines[:].set_visible(False) |
| 37 | + ax.tick_params(left=False) |
| 38 | + |
| 39 | + |
| 40 | +built_in_color_sequences = [ |
| 41 | + 'tab10', 'tab20', 'tab20b', 'tab20c', 'Pastel1', 'Pastel2', 'Paired', |
| 42 | + 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'petroff10'] |
| 43 | + |
| 44 | + |
| 45 | +fig, ax = plt.subplots(figsize=(6.4, 9.6), layout='constrained') |
| 46 | + |
| 47 | +plot_color_sequences(built_in_color_sequences, ax) |
| 48 | +ax.set_title('Built In Color Sequences') |
| 49 | + |
| 50 | +plt.show() |
| 51 | + |
| 52 | + |
| 53 | +# %% |
| 54 | +# |
| 55 | +# .. admonition:: References |
| 56 | +# |
| 57 | +# The use of the following functions, methods, classes and modules is shown |
| 58 | +# in this example: |
| 59 | +# |
| 60 | +# - `matplotlib.colors.ColorSequenceRegistry` |
| 61 | +# - `matplotlib.axes.Axes.scatter` |
| 62 | +# |
| 63 | +# .. tags:: |
| 64 | +# |
| 65 | +# styling: color |
| 66 | +# purpose: reference |
0 commit comments