|
3 | 3 | Color Demo
|
4 | 4 | ==========
|
5 | 5 |
|
6 |
| -matplotlib gives you 5 ways to specify colors, |
| 6 | +Matplotlib gives you 8 ways to specify colors, |
7 | 7 |
|
8 |
| - 1) as a single letter string, ala MATLAB |
| 8 | +1) an RGB or RGBA tuple of float values in ``[0, 1]`` (e.g. ``(0.1, 0.2, 0.5)`` |
| 9 | + or ``(0.1, 0.2, 0.5, 0.3)``). RGBA is short for Red, Green, Blue, Alpha; |
| 10 | +2) a hex RGB or RGBA string (e.g., ``'#0F0F0F'`` or ``'#0F0F0F0F'``); |
| 11 | +3) a string representation of a float value in ``[0, 1]`` inclusive for gray |
| 12 | + level (e.g., ``'0.5'``); |
| 13 | +4) a single letter string, i.e. one of |
| 14 | + ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}``; |
| 15 | +5) a X11/CSS4 ("html") color name, e.g. ``"blue"``; |
| 16 | +6) a name from the `xkcd color survey <https://xkcd.com/color/rgb/>`__, |
| 17 | + prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``); |
| 18 | +7) a "Cn" color spec, i.e. `'C'` followed by a single digit, which is an index |
| 19 | + into the default property cycle |
| 20 | + (``matplotlib.rcParams['axes.prop_cycle']``); the indexing occurs at artist |
| 21 | + creation time and defaults to black if the cycle does not include color. |
| 22 | +8) one of ``{'tab:blue', 'tab:orange', 'tab:green', |
| 23 | + 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', |
| 24 | + 'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the |
| 25 | + 'tab10' categorical palette (which is the default color cycle); |
9 | 26 |
|
10 |
| - 2) as an html style hex string or html color name |
| 27 | +For more information on colors in matplotlib see |
11 | 28 |
|
12 |
| - 3) as an R,G,B tuple, where R,G,B, range from 0-1 |
13 |
| -
|
14 |
| - 4) as a string representing a floating point number |
15 |
| - from 0 to 1, corresponding to shades of gray. |
16 |
| -
|
17 |
| - 5) as a special color "Cn", where n is a number 0-9 specifying the |
18 |
| - nth color in the currently active color cycle. |
19 |
| -
|
20 |
| -See help(colors) for more info. |
| 29 | +* the :ref:`sphx_glr_tutorials_colors_colors.py` tutorial; |
| 30 | +* the `matplotlib.colors` API; |
| 31 | +* the :ref:`sphx_glr_gallery_color_named_colors.py` example. |
21 | 32 | """
|
| 33 | + |
22 | 34 | import matplotlib.pyplot as plt
|
23 | 35 | import numpy as np
|
24 | 36 |
|
25 |
| -t = np.arange(0.0, 2.0, 0.01) |
| 37 | +t = np.linspace(0.0, 2.0, 201) |
26 | 38 | s = np.sin(2 * np.pi * t)
|
27 | 39 |
|
28 |
| -fig, ax = plt.subplots(facecolor='darkslategray') |
29 |
| -ax.plot(t, s, 'C1') |
30 |
| -ax.set_xlabel('time (s)', color='C1') |
31 |
| -ax.set_ylabel('voltage (mV)', color='0.5') # grayscale color |
32 |
| -ax.set_title('About as silly as it gets, folks', color='#afeeee') |
| 40 | +# 1) RGB tuple: |
| 41 | +fig, ax = plt.subplots(facecolor=(.18, .31, .31)) |
| 42 | +# 2) hex string: |
| 43 | +ax.set_facecolor('#eafff5') |
| 44 | +# 3) gray level string: |
| 45 | +ax.set_title('Voltage vs. time chart', color='0.7') |
| 46 | +# 4) single letter color string |
| 47 | +ax.set_xlabel('time (s)', color='c') |
| 48 | +# 5) a named color: |
| 49 | +ax.set_ylabel('voltage (mV)', color='peachpuff') |
| 50 | +# 6) a named xkcd color: |
| 51 | +ax.plot(t, s, 'xkcd:crimson') |
| 52 | +# 7) Cn notation: |
| 53 | +ax.plot(t, .7*s, color='C4', linestyle='--') |
| 54 | +# 8) tab notation: |
| 55 | +ax.tick_params(labelcolor='tab:orange') |
| 56 | + |
33 | 57 |
|
34 | 58 | plt.show()
|
| 59 | + |
| 60 | +############################################################################# |
| 61 | +# |
| 62 | +# ------------ |
| 63 | +# |
| 64 | +# References |
| 65 | +# """""""""" |
| 66 | +# |
| 67 | +# The use of the following functions, methods, classes and modules is shown |
| 68 | +# in this example: |
| 69 | + |
| 70 | +import matplotlib |
| 71 | +matplotlib.colors |
| 72 | +matplotlib.axes.Axes.plot |
| 73 | +matplotlib.axes.Axes.set_facecolor |
| 74 | +matplotlib.axes.Axes.set_title |
| 75 | +matplotlib.axes.Axes.set_xlabel |
| 76 | +matplotlib.axes.Axes.set_ylabel |
| 77 | +matplotlib.axes.Axes.tick_params |
0 commit comments