Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
ENH: Add option to define a color as color=(some_color, some_alpha)#24691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletionsdoc/users/next_whats_new/new_color_spec_tuple.rst
This file contains hidden or 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)*. |
53 changes: 53 additions & 0 deletionsgalleries/examples/color/set_alpha.py
This file contains hidden or 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` |
3 changes: 3 additions & 0 deletionsgalleries/users_explain/colors/colors.py
This file contains hidden or 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
19 changes: 17 additions & 2 deletionslib/matplotlib/colors.py
This file contains hidden or 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
45 changes: 45 additions & 0 deletionslib/matplotlib/tests/test_colors.py
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.