| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,11 @@ | |||
| 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,45 +77,57 @@ | |||
| 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. | ||
| 80 | + Transparency | ||
| 81 | + ============ | ||
| 83 | 82 | ||
| 84 | - The two Artists combine with alpha compositing. Matplotlib uses the equation | ||
| 85 | - below to compute the result of blending a new Artist. | ||
| 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. | ||
| 86 | 86 | ||
| 87 | - :: | ||
| 87 | + The *alpha* value determines the resulting color by blending the | ||
| 88 | + foreground color with the background color according to the formula | ||
| 88 | 89 | ||
| 89 | - RGB_{new} = RGB_{below} * (1 - \\alpha) + RGB_{artist} * \\alpha | ||
| 90 | + .. math:: | ||
| 90 | 91 | ||
| 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. | ||
| 92 | + RGB_{result} = RGB_{background} * (1 - \\alpha) + RGB_{foreground} * \\alpha | ||
| 95 | 93 | ||
| 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 | - +---------------+-------------------------------------------------------------+ | ||
| 103 | - | ||
| 104 | - .. note:: | ||
| 94 | + The following plot illustrates the effect of transparency. | ||
| 95 | + """ | ||
| 105 | 96 | ||
| 106 | - Changing the order of Artists will generally change the resulting figure. | ||
| 97 | + import matplotlib.pyplot as plt | ||
| 98 | + from matplotlib.patches import Rectangle | ||
| 99 | + import numpy as np | ||
| 107 | 100 | ||
| 101 | + fig, ax = plt.subplots(figsize=(6.5, 1.65), layout='constrained') | ||
| 102 | + ax.add_patch(Rectangle((-0.2, -0.35), 11.2, 0.7, color='C1', alpha=0.8)) | ||
| 103 | + for i, alpha in enumerate(np.linspace(0, 1, 11)): | ||
| 104 | + ax.add_patch(Rectangle((i, 0.05), 0.8, 0.6, alpha=alpha, zorder=0)) | ||
| 105 | + ax.text(i+0.4, 0.85, f"{alpha:.1f}", ha='center') | ||
| 106 | + ax.add_patch(Rectangle((i, -0.05), 0.8, -0.6, alpha=alpha, zorder=2)) | ||
| 107 | + ax.set_xlim(-0.2, 13) | ||
| 108 | + ax.set_ylim(-1, 1) | ||
| 109 | + ax.set_title('alpha values') | ||
| 110 | + ax.text(11.3, 0.6, 'zorder=1', va='center', color='C0') | ||
| 111 | + ax.text(11.3, 0, 'zorder=2\nalpha=0.8', va='center', color='C1') | ||
| 112 | + ax.text(11.3, -0.6, 'zorder=3', va='center', color='C0') | ||
| 113 | + ax.axis('off') | ||
| 108 | 114 | ||
| 109 | - "CN" color selection | ||
| 110 | - -------------------- | ||
| 111 | 115 | ||
| 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 | - """ | ||
| 116 | + ############################################################################### | ||
| 117 | + # | ||
| 118 | + # The orange rectangle is semi-transparent with *alpha* = 0.8. The top row of | ||
| 119 | + # blue squares is drawn below and the bottom row of blue squares is drawn on | ||
| 120 | + # top of the orange rectangle. | ||
| 121 | + # | ||
| 122 | + # See also :doc:`/gallery/misc/zorder_demo` to learn more on the drawing order. | ||
| 123 | + # | ||
| 124 | + # | ||
| 125 | + # "CN" color selection | ||
| 126 | + # ==================== | ||
| 127 | + # | ||
| 128 | + # Matplotlib converts "CN" colors to RGBA when drawing Artists. The | ||
| 129 | + # :doc:`/tutorials/intermediate/color_cycle` section contains additional | ||
| 130 | + # information about controlling colors and style properties. | ||
| 116 | 131 | ||
| 117 | 132 | ||
| 118 | 133 | import numpy as np | |
@@ -144,7 +159,7 @@ def demo(sty): | |||
| 144 | 159 | # .. _xkcd-colors: | |
| 145 | 160 | # | |
| 146 | 161 | # Comparison between X11/CSS4 and xkcd colors | |
| 147 | - # ------------------------------------------- | ||
| 162 | + # =========================================== | ||
| 148 | 163 | # | |
| 149 | 164 | # The xkcd colors come from a `user survey conducted by the webcomic xkcd | |
| 150 | 165 | # <https://blog.xkcd.com/2010/05/03/color-survey-results/>`__. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments