Skip to content

Commit fee17d7

Browse files
committed
DOC: Clarify transparency in colors
1 parent c1c4b5a commit fee17d7

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

tutorials/colors/colors.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""
22
*****************
3-
Specifying Colors
3+
Specifying colors
44
*****************
55
6+
Color formats
7+
=============
8+
69
Matplotlib recognizes the following formats to specify a color.
710
811
+--------------------------------------+--------------------------------------+
@@ -74,45 +77,46 @@
7477
"Red", "Green", and "Blue" are the intensities of those colors. In combination,
7578
they represent the colorspace.
7679
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+
============
8682
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).
8887
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
9090
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::
9592
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
10394
104-
.. note::
95+
The following plot shows an orange rectangle in the background and a set of
96+
blue rectangles with varying transparency in the front:
97+
"""
10598

106-
Changing the order of Artists will generally change the resulting figure.
99+
import matplotlib.pyplot as plt
100+
from matplotlib.patches import Rectangle
101+
import numpy as np
107102

103+
fig, ax = plt.subplots(figsize=(6.5, 1.1), layout='constrained')
104+
ax.add_patch(Rectangle((-0.2, 0), 11.2, 0.5, color='C1'))
105+
for i, alpha in enumerate(np.linspace(0, 1, 11)):
106+
ax.add_patch(Rectangle((i, 0.2), 0.8, 0.6, alpha=alpha, zorder=0))
107+
ax.text(i+0.4, 0.85, f"{alpha:.1f}", ha='center')
108+
ax.set_xlim(-0.2, 11)
109+
ax.set_title('alpha values')
110+
ax.axis('off')
108111

109-
"CN" color selection
110-
--------------------
111112

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-
"""
113+
###############################################################################
114+
# "CN" color selection
115+
# ====================
116+
#
117+
# Matplotlib converts "CN" colors to RGBA when drawing Artists. The
118+
# :doc:`/tutorials/intermediate/color_cycle` section contains additional
119+
# information about controlling colors and style properties.
116120

117121

118122
import numpy as np
@@ -144,7 +148,7 @@ def demo(sty):
144148
# .. _xkcd-colors:
145149
#
146150
# Comparison between X11/CSS4 and xkcd colors
147-
# -------------------------------------------
151+
# ===========================================
148152
#
149153
# The xkcd colors come from a `user survey conducted by the webcomic xkcd
150154
# <https://blog.xkcd.com/2010/05/03/color-survey-results/>`__.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy