|
7 | 7 | cycle provides. Selecting individual colors from one of the provided colormaps can be a
|
8 | 8 | convenient way to do this.
|
9 | 9 |
|
10 |
| -Once we have hold of a `.Colormap` instance, the individual colors can be accessed |
11 |
| -by passing it an index. If we want a specific number of colors taken at regular |
12 |
| -intervals from a continuous colormap, we can create a new colormap using the |
13 |
| -`~.Colormap.resampled` method. |
| 10 | +We can retrieve colors from any `.Colormap` by calling it with a float or a list of |
| 11 | +floats in the range [0, 1]; e.g. ``cmap(0.5)`` will give the middle color. See also |
| 12 | +`.Colormap.__call__`. |
14 | 13 |
|
15 |
| -For more details about manipulating colormaps, see :ref:`colormap-manipulation`. |
| 14 | +Extracting colors from a continuous colormap |
| 15 | +-------------------------------------------- |
16 | 16 | """
|
17 | 17 |
|
18 | 18 | importmatplotlib.pyplotasplt
|
| 19 | +importnumpyasnp |
19 | 20 |
|
20 | 21 | importmatplotlibasmpl
|
21 | 22 |
|
22 | 23 | n_lines=21
|
| 24 | +cmap=mpl.colormaps['plasma'] |
23 | 25 |
|
24 |
| -cmap=mpl.colormaps.get_cmap('plasma').resampled(n_lines) |
| 26 | +# Take colors at regular intervals spanning the colormap. |
| 27 | +colors=cmap(np.linspace(0,1,n_lines)) |
25 | 28 |
|
26 | 29 | fig,ax=plt.subplots(layout='constrained')
|
27 | 30 |
|
28 |
| -foriinrange(n_lines): |
29 |
| -ax.plot([0,i],color=cmap(i)) |
| 31 | +fori,colorinenumerate(colors): |
| 32 | +ax.plot([0,i],color=color) |
30 | 33 |
|
31 | 34 | plt.show()
|
32 | 35 |
|
33 | 36 | # %%
|
34 |
| -# Instead of passing colors one by one to `~.Axes.plot`, we can replace the default |
35 |
| -# color cycle with a different set of colors. Specifying a `~cycler.cycler` instance |
36 |
| -# within `.rcParams` achieves that. See :ref:`color_cycle` for details. |
37 |
| - |
38 |
| - |
39 |
| -fromcyclerimportcycler |
40 |
| - |
41 |
| -cmap=mpl.colormaps.get_cmap('Dark2') |
42 |
| -colors=cmap(range(cmap.N))# cmap.N is number of unique colors in the colormap |
| 37 | +# |
| 38 | +# Extracting colors from a discrete colormap |
| 39 | +# ------------------------------------------ |
| 40 | +# The list of all colors in a `.ListedColormap` is available as the ``colors`` |
| 41 | +# attribute. |
43 | 42 |
|
44 |
| -withmpl.rc_context({'axes.prop_cycle':cycler(color=colors)}): |
| 43 | +colors=mpl.colormaps['Dark2'].colors |
45 | 44 |
|
46 |
| -fig,ax=plt.subplots(layout='constrained') |
| 45 | +fig,ax=plt.subplots(layout='constrained') |
47 | 46 |
|
48 |
| -foriinrange(n_lines): |
49 |
| -ax.plot([0,i]) |
| 47 | +fori,colorinenumerate(colors): |
| 48 | +ax.plot([0,i],color=color) |
50 | 49 |
|
51 | 50 | plt.show()
|
52 | 51 |
|
53 | 52 | # %%
|
| 53 | +# See Also |
| 54 | +# -------- |
| 55 | +# |
| 56 | +# For more details about manipulating colormaps, see :ref:`colormap-manipulation`. To |
| 57 | +# change the default color cycle, see :ref:`color_cycle`. |
54 | 58 | #
|
55 | 59 | # .. admonition:: References
|
56 | 60 | #
|
|