forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request matplotlib#24691 from j1642/patch_alpha
ENH: Add option to define a color as color=(some_color, some_alpha)
- Loading branch information
Showing
5 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Add a new valid color format ``(matplotlib_color, alpha)`` | ||
---------------------------------------------------------- | ||
|
||
|
||
.. plot:: | ||
:include-source: true | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.patches import Rectangle | ||
|
||
fig, ax = plt.subplots() | ||
|
||
rectangle = Rectangle((.2, .2), .6, .6, | ||
facecolor=('blue', 0.2), | ||
edgecolor=('green', 0.5)) | ||
ax.add_patch(rectangle) | ||
|
||
|
||
Users can define a color using the new color specification, *(matplotlib_color, alpha)*. | ||
Note that an explicit alpha keyword argument will override an alpha value from | ||
*(matplotlib_color, alpha)*. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
================================= | ||
Ways to set a color's alpha value | ||
================================= | ||
Compare setting alpha by the *alpha* keyword argument and by one of the Matplotlib color | ||
formats. Often, the *alpha* keyword is the only tool needed to add transparency to a | ||
color. In some cases, the *(matplotlib_color, alpha)* color format provides an easy way | ||
to fine-tune the appearance of a Figure. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
# Fixing random state for reproducibility. | ||
np.random.seed(19680801) | ||
|
||
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4)) | ||
|
||
x_values = [n for n in range(20)] | ||
y_values = np.random.randn(20) | ||
|
||
facecolors = ['green' if y > 0 else 'red' for y in y_values] | ||
edgecolors = facecolors | ||
|
||
ax1.bar(x_values, y_values, color=facecolors, edgecolor=edgecolors, alpha=0.5) | ||
ax1.set_title("Explicit 'alpha' keyword value\nshared by all bars and edges") | ||
|
||
|
||
# Normalize y values to get distinct face alpha values. | ||
abs_y = [abs(y) for y in y_values] | ||
face_alphas = [n / max(abs_y) for n in abs_y] | ||
edge_alphas = [1 - alpha for alpha in face_alphas] | ||
|
||
colors_with_alphas = list(zip(facecolors, face_alphas)) | ||
edgecolors_with_alphas = list(zip(edgecolors, edge_alphas)) | ||
|
||
ax2.bar(x_values, y_values, color=colors_with_alphas, | ||
edgecolor=edgecolors_with_alphas) | ||
ax2.set_title('Normalized alphas for\neach bar and each edge') | ||
|
||
plt.show() | ||
|
||
# %% | ||
# | ||
# .. admonition:: References | ||
# | ||
# The use of the following functions, methods, classes and modules is shown | ||
# in this example: | ||
# | ||
# - `matplotlib.axes.Axes.bar` | ||
# - `matplotlib.pyplot.subplots` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters