1010from matplotlib .artist import setp
1111import numpy as np
1212
13+ from pandas ._libs import lib
1314from pandas .util ._decorators import cache_readonly
1415from pandas .util ._exceptions import find_stack_level
1516
@@ -113,26 +114,26 @@ def _plot( # type: ignore[override]
113114 else :
114115 return ax , bp
115116
116- def _validate_color_args (self ):
117- if "color" in self .kwds :
118- if self .colormap is not None :
119- warnings .warn (
120- "'color' and 'colormap' cannot be used "
121- "simultaneously. Using 'color'" ,
122- stacklevel = find_stack_level (),
123- )
124- self .color = self .kwds .pop ("color" )
117+ def _validate_color_args (self , color , colormap ):
118+ if color is lib .no_default :
119+ return None
125120
126- if isinstance (self .color , dict ):
127- valid_keys = ["boxes" , "whiskers" , "medians" , "caps" ]
128- for key in self .color :
129- if key not in valid_keys :
130- raise ValueError (
131- f"color dict contains invalid key '{ key } '. "
132- f"The key must be either { valid_keys } "
133- )
134- else :
135- self .color = None
121+ if colormap is not None :
122+ warnings .warn (
123+ "'color' and 'colormap' cannot be used "
124+ "simultaneously. Using 'color'" ,
125+ stacklevel = find_stack_level (),
126+ )
127+
128+ if isinstance (color , dict ):
129+ valid_keys = ["boxes" , "whiskers" , "medians" , "caps" ]
130+ for key in color :
131+ if key not in valid_keys :
132+ raise ValueError (
133+ f"color dict contains invalid key '{ key } '. "
134+ f"The key must be either { valid_keys } "
135+ )
136+ return color
136137
137138 @cache_readonly
138139 def _color_attrs (self ):
@@ -182,16 +183,8 @@ def maybe_color_bp(self, bp) -> None:
182183 medians = self .color or self ._medians_c
183184 caps = self .color or self ._caps_c
184185
185- # GH 30346, when users specifying those arguments explicitly, our defaults
186- # for these four kwargs should be overridden; if not, use Pandas settings
187- if not self .kwds .get ("boxprops" ):
188- setp (bp ["boxes" ], color = boxes , alpha = 1 )
189- if not self .kwds .get ("whiskerprops" ):
190- setp (bp ["whiskers" ], color = whiskers , alpha = 1 )
191- if not self .kwds .get ("medianprops" ):
192- setp (bp ["medians" ], color = medians , alpha = 1 )
193- if not self .kwds .get ("capprops" ):
194- setp (bp ["caps" ], color = caps , alpha = 1 )
186+ color_tup = (boxes , whiskers , medians , caps )
187+ maybe_color_bp (bp , color_tup = color_tup , ** self .kwds )
195188
196189 def _make_plot (self , fig : Figure ) -> None :
197190 if self .subplots :
@@ -276,6 +269,19 @@ def result(self):
276269 return self ._return_obj
277270
278271
272+ def maybe_color_bp (bp , color_tup , ** kwds ) -> None :
273+ # GH#30346, when users specifying those arguments explicitly, our defaults
274+ # for these four kwargs should be overridden; if not, use Pandas settings
275+ if not kwds .get ("boxprops" ):
276+ setp (bp ["boxes" ], color = color_tup [0 ], alpha = 1 )
277+ if not kwds .get ("whiskerprops" ):
278+ setp (bp ["whiskers" ], color = color_tup [1 ], alpha = 1 )
279+ if not kwds .get ("medianprops" ):
280+ setp (bp ["medians" ], color = color_tup [2 ], alpha = 1 )
281+ if not kwds .get ("capprops" ):
282+ setp (bp ["caps" ], color = color_tup [3 ], alpha = 1 )
283+
284+
279285def _grouped_plot_by_column (
280286 plotf ,
281287 data ,
@@ -389,18 +395,6 @@ def _get_colors():
389395
390396 return result
391397
392- def maybe_color_bp (bp , ** kwds ) -> None :
393- # GH 30346, when users specifying those arguments explicitly, our defaults
394- # for these four kwargs should be overridden; if not, use Pandas settings
395- if not kwds .get ("boxprops" ):
396- setp (bp ["boxes" ], color = colors [0 ], alpha = 1 )
397- if not kwds .get ("whiskerprops" ):
398- setp (bp ["whiskers" ], color = colors [1 ], alpha = 1 )
399- if not kwds .get ("medianprops" ):
400- setp (bp ["medians" ], color = colors [2 ], alpha = 1 )
401- if not kwds .get ("capprops" ):
402- setp (bp ["caps" ], color = colors [3 ], alpha = 1 )
403-
404398 def plot_group (keys , values , ax : Axes , ** kwds ):
405399 # GH 45465: xlabel/ylabel need to be popped out before plotting happens
406400 xlabel , ylabel = kwds .pop ("xlabel" , None ), kwds .pop ("ylabel" , None )
@@ -419,7 +413,7 @@ def plot_group(keys, values, ax: Axes, **kwds):
419413 _set_ticklabels (
420414 ax = ax , labels = keys , is_vertical = kwds .get ("vert" , True ), rotation = rot
421415 )
422- maybe_color_bp (bp , ** kwds )
416+ maybe_color_bp (bp , color_tup = colors , ** kwds )
423417
424418 # Return axes in multiplot case, maybe revisit later # 985
425419 if return_type == "dict" :
0 commit comments