-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Creating_parse_bar_color_args to unify color handling in plt.bar with precedence and sequence support for facecolor and edgecolor #29133
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
Changes from 1 commit
cde62aa
bca9d43
ab2253f
88bc7a1
6193498
94db14e
f92aa1a
4959889
ef746a3
42f5a71
314a3dc
062dda1
ee98b4f
08741ef
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2320,7 +2320,59 @@ | |||||||
# we do by default and convert dx by itself. | ||||||||
dx = convert(dx) | ||||||||
return dx | ||||||||
|
||||||||
@staticmethod | ||||||||
def _parse_bar_color_args(kwargs, get_next_color_func): | ||||||||
""" | ||||||||
Helper function to process color-related arguments of `.Axes.bar`. | ||||||||
|
||||||||
Argument precedence for facecolors: | ||||||||
|
||||||||
- kwargs['facecolor'] | ||||||||
- kwargs['color'] | ||||||||
- 'b' if in classic mode else the result of ``get_next_color_func()`` | ||||||||
|
||||||||
Argument precedence for edgecolors: | ||||||||
|
||||||||
- kwargs['edgecolor'] | ||||||||
- kwargs['color'] | ||||||||
- None | ||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
kwargs : dict | ||||||||
Additional kwargs. If these keys exist, we pop and process them: | ||||||||
'facecolor', 'edgecolor', 'color' | ||||||||
Note: The dict is modified by this function. | ||||||||
get_next_color_func : callable | ||||||||
A callable that returns a color. This color is used as facecolor | ||||||||
if no other color is provided. | ||||||||
|
||||||||
Returns | ||||||||
------- | ||||||||
facecolor | ||||||||
The facecolor. | ||||||||
timhoffm marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
edgecolor | ||||||||
The edgecolor. | ||||||||
timhoffm marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
""" | ||||||||
color = kwargs.pop('color', None) | ||||||||
|
||||||||
facecolor = kwargs.pop('facecolor', color) | ||||||||
edgecolor = kwargs.pop('edgecolor', color) | ||||||||
|
||||||||
facecolor = (facecolor if facecolor is not None | ||||||||
else "b" if mpl.rcParams['_internal.classic_mode'] | ||||||||
Check failure on line 2364 in lib/matplotlib/axes/_axes.py
|
||||||||
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. I guess you made "b" the default in classic mode for consistency with |
||||||||
else get_next_color_func()) | ||||||||
Check failure on line 2365 in lib/matplotlib/axes/_axes.py
|
||||||||
|
||||||||
try: | ||||||||
facecolor = mcolors.to_rgba_array(facecolor) | ||||||||
except ValueError as err: | ||||||||
raise ValueError( | ||||||||
"'facecolor' or 'color' argument must be a valid color or sequence of colors." | ||||||||
) from err | ||||||||
|
||||||||
return facecolor, edgecolor | ||||||||
|
||||||||
@_preprocess_data() | ||||||||
@_docstring.interpd | ||||||||
def bar(self, x, height, width=0.8, bottom=None, *, align="center", | ||||||||
|
@@ -2376,6 +2428,9 @@ | |||||||
Other Parameters | ||||||||
---------------- | ||||||||
color : :mpltype:`color` or list of :mpltype:`color`, optional | ||||||||
The colors of the bar faces and edges. | ||||||||
|
||||||||
facecolor : :mpltype:`color` or list of :mpltype:`color`, optional | ||||||||
The colors of the bar faces. | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
edgecolor : :mpltype:`color` or list of :mpltype:`color`, optional | ||||||||
|
@@ -2441,10 +2496,8 @@ | |||||||
bar. See :doc:`/gallery/lines_bars_and_markers/bar_stacked`. | ||||||||
""" | ||||||||
kwargs = cbook.normalize_kwargs(kwargs, mpatches.Patch) | ||||||||
color = kwargs.pop('color', None) | ||||||||
if color is None: | ||||||||
color = self._get_patches_for_fill.get_next_color() | ||||||||
edgecolor = kwargs.pop('edgecolor', None) | ||||||||
color, edgecolor = self._parse_bar_color_args(kwargs, self._get_patches_for_fill.get_next_color) | ||||||||
|
||||||||
linewidth = kwargs.pop('linewidth', None) | ||||||||
hatch = kwargs.pop('hatch', None) | ||||||||
|
||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
According to #12739 the method for parsing
scatter
color arguments was made static so that we could have unit tests specifically for that method. Do you plan to write similar tests here? If not, this could be an ordinary method and we would not need to pass theget_next_color_func
but just accessself._get_patches_for_fill.get_next_color
directly.Edit: looking at the discussion in #12739 I think it is not advisable to write such tests but just test the public
bar
function.