|
1 | 1 | """
|
2 | 2 | *****************
|
3 |
| -Specifying Colors |
| 3 | +Specifying colors |
4 | 4 | *****************
|
5 | 5 |
|
| 6 | +Color formats |
| 7 | +============= |
| 8 | +
|
6 | 9 | Matplotlib recognizes the following formats to specify a color.
|
7 | 10 |
|
8 | 11 | +--------------------------------------+--------------------------------------+
|
|
74 | 77 | "Red", "Green", and "Blue" are the intensities of those colors. In combination,
|
75 | 78 | they represent the colorspace.
|
76 | 79 |
|
77 |
| -Matplotlib draws Artists based on the ``zorder`` parameter. If there are no |
78 |
| -specified values, Matplotlib defaults to the order of the Artists added to the |
79 |
| -Axes. |
80 |
| -
|
81 |
| -The alpha for an Artist controls opacity. It indicates how the RGB color of the |
82 |
| -new Artist combines with RGB colors already on the Axes. |
83 |
| -
|
84 |
| -The two Artists combine with alpha compositing. Matplotlib uses the equation |
85 |
| -below to compute the result of blending a new Artist. |
| 80 | +Transparency |
| 81 | +============ |
86 | 82 |
|
87 |
| -:: |
| 83 | +The *alpha* value of a color specifies its transparency, where 0 is fully |
| 84 | +transparent and 1 is fully opaque. When a color is semi-transparent, the |
| 85 | +background color will show through (see also :doc:`/gallery/misc/zorder_demo` |
| 86 | +to learn more about the drawing order). |
88 | 87 |
|
89 |
| - RGB_{new} = RGB_{below} * (1 - \\alpha) + RGB_{artist} * \\alpha |
| 88 | +The *alpha* value determines the resulting color by blending the |
| 89 | +foreground color with the background color according to the formula |
90 | 90 |
|
91 |
| -Alpha of 1 indicates the new Artist completely covers the previous color. |
92 |
| -Alpha of 0 for top color is not visible; however, it contributes to blending |
93 |
| -for intermediate values as the cumulative result of all previous Artists. The |
94 |
| -following table contains examples. |
| 91 | +.. math:: |
95 | 92 |
|
96 |
| -+---------------+-------------------------------------------------------------+ |
97 |
| -| Alpha value | Visual | |
98 |
| -+===============+=============================================================+ |
99 |
| -| ``0.3`` | .. image:: ../../_static/color_zorder_A.png | |
100 |
| -+---------------+-------------------------------------------------------------+ |
101 |
| -| ``1`` | .. image:: ../../_static/color_zorder_B.png | |
102 |
| -+---------------+-------------------------------------------------------------+ |
| 93 | + RGB_{result} = RGB_{background} * (1 - \\alpha) + RGB_{foreground} * \\alpha |
103 | 94 |
|
104 |
| -.. note:: |
105 |
| -
|
106 |
| - Changing the order of Artists will generally change the resulting figure. |
| 95 | +Obviously, the resulting color depends on the draw order of the artists: |
| 96 | +""" |
107 | 97 |
|
| 98 | +import matplotlib.pyplot as plt |
| 99 | +from matplotlib.patches import Rectangle |
| 100 | + |
| 101 | +fig, ax = plt.subplots() |
| 102 | +ax.add_patch(Rectangle((0.08, 0.3), 0.4, 0.6, color='b', zorder=1, alpha=0.8)) |
| 103 | +ax.add_patch(Rectangle((0.3, 0.1), 0.4, 0.6, color='r', zorder=2, alpha=0.8)) |
| 104 | +ax.add_patch(Rectangle((0.52, 0.3), 0.4, 0.6, color='b', zorder=3, alpha=0.8)) |
| 105 | +ax.text(0.29, 0.8, "zorder=1", ha='center', color='white') |
| 106 | +ax.text(0.5, 0.2, "zorder=2", ha='center', color='white') |
| 107 | +ax.text(0.71, 0.8, "zorder=3", ha='center', color='white') |
| 108 | +ax.set_title('alpha=0.8') |
| 109 | +ax.axis('off') |
108 | 110 |
|
109 |
| -"CN" color selection |
110 |
| --------------------- |
111 | 111 |
|
112 |
| -Matplotlib converts "CN" colors to RGBA when drawing Artists. The |
113 |
| -:doc:`/tutorials/intermediate/color_cycle` section contains additional |
114 |
| -information about controlling colors and style properties. |
115 |
| -""" |
| 112 | +############################################################################### |
| 113 | +# "CN" color selection |
| 114 | +# ==================== |
| 115 | +# |
| 116 | +# Matplotlib converts "CN" colors to RGBA when drawing Artists. The |
| 117 | +# :doc:`/tutorials/intermediate/color_cycle` section contains additional |
| 118 | +# information about controlling colors and style properties. |
116 | 119 |
|
117 | 120 |
|
118 | 121 | import numpy as np
|
@@ -144,7 +147,7 @@ def demo(sty):
|
144 | 147 | # .. _xkcd-colors:
|
145 | 148 | #
|
146 | 149 | # Comparison between X11/CSS4 and xkcd colors
|
147 |
| -# ------------------------------------------- |
| 150 | +# =========================================== |
148 | 151 | #
|
149 | 152 | # The xkcd colors come from a `user survey conducted by the webcomic xkcd
|
150 | 153 | # <https://blog.xkcd.com/2010/05/03/color-survey-results/>`__.
|
|
0 commit comments