-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Enh color shadding #8895
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enh color shadding #8895
Changes from all commits
b0d9da3
a9bc899
8ada83e
e5957e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Easy color shading | ||
`````````````````` | ||
Often we would like the ability add subtle color variation to our plots, | ||
especially when similar element are plotted in close proximity to one another. | ||
:func:`matplotlib.colors.shade_color` can be used to take an existing color and | ||
slightly darken it, by giving a negative percentage, or to slightly lighten it, | ||
by giving a positive percentage. | ||
|
||
.. plot:: mpl_examples/color/color_shade_demo.py |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
Demo of shade_color utility. | ||
|
||
""" | ||
import matplotlib.pyplot as plt | ||
import matplotlib.colors as mcolors | ||
|
||
fig = plt.figure() | ||
ax = fig.add_subplot(111) | ||
|
||
lightened_color = mcolors.shade_color('blue', -50) | ||
darkened_color = mcolors.shade_color('blue', +50) | ||
|
||
ax.fill_between([0,1], [0,0], [1,1], facecolor=darkened_color) | ||
ax.fill_between([0,1], [0,0], [.66, .66], facecolor='blue') | ||
ax.fill_between([0,1], [0,0], [.33, .33], facecolor=lightened_color) | ||
|
||
plt.xlim([0, 1]) | ||
plt.ylim([0, 1]) | ||
|
||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,9 +64,12 @@ | |
from collections import Sized | ||
import re | ||
import warnings | ||
from colorsys import rgb_to_hls, hls_to_rgb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Importing these publicly is a bad idea, because it makes |
||
|
||
|
||
import numpy as np | ||
import matplotlib.cbook as cbook | ||
|
||
from ._color_data import BASE_COLORS, TABLEAU_COLORS, CSS4_COLORS, XKCD_COLORS | ||
|
||
|
||
|
@@ -1947,3 +1950,38 @@ def from_levels_and_colors(levels, colors, extend='neither'): | |
|
||
norm = BoundaryNorm(levels, ncolors=n_data_colors) | ||
return cmap, norm | ||
|
||
|
||
def shade_color(color, percent): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In almost all of the other color code we handle fractions as floats in range |
||
"""A color helper utility to either darken or lighten given color. | ||
|
||
This color utility function allows the user to easily darken or | ||
lighten a color for plotting purposes. This function first | ||
converts the given color to RGB using ColorConverter and then to | ||
HSL. The saturation is modified according to the given percentage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Genuine remark about what may be a typo: it is the lightness ("l" in "hls") that is modified by this function, not the saturation ("s" in "hls"), isn't it? |
||
and converted back to RGB. | ||
|
||
Parameters | ||
---------- | ||
color : string, list, hexvalue | ||
Any acceptable Matplotlib color value, such as 'red', | ||
'slategrey', '#FFEE11', (1,0,0) | ||
|
||
percent : the amount by which to brighten or darken the color. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specify units and range |
||
|
||
Returns | ||
------- | ||
color : tuple of floats | ||
tuple representing converted rgb values | ||
|
||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should do some validation on the scaling |
||
rgb = colorConverter.to_rgb(color) | ||
|
||
h, l, s = rgb_to_hls(*rgb) | ||
|
||
l *= 1 + float(percent)/100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems reasonable. |
||
|
||
l = np.clip(l, 0, 1) | ||
|
||
return hls_to_rgb(h, l, s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be
----