Pandas - problems with boxplot color settings

I am running Pandas 0.16.2 and Matplotlib 1.4.3. I have this problem coloring the boxplot median generated by the following code:

df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) fig, ax = plt.subplots() medianprops = dict(linestyle='-', linewidth=2, color='blue') bp = df.boxplot(medianprops=medianprops) plt.show() 

This returns:

enter image description here

It appears that the color parameter is not readable. By changing only linestyle settings and line widths, the plot responds correctly.

 medianprops = dict(linestyle='-.', linewidth=5, color='blue') 

enter image description here

Can anyone reproduce it?

+6
source share
2 answers

In fact, the following workaround works well by returning a dict from the boxplot command:

 df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) fig, ax = plt.subplots() bp = df.boxplot(return_type='dict') 

and then assign the direct colors and line widths to the media with:

 [[item.set_color('r') for item in bp[key]['medians']] for key in bp.keys()] [[item.set_linewidth(0.8) for item in bp[key]['medians']] for key in bp.keys()] 
0
source

DataFrame.boxplot() looked at the code for DataFrame.boxplot() , there is a special code for processing the colors of various elements that replace the kws passed to matplotlib boxplot . Theoretically, there seems to be a way to pass the color= argument containing a dictionary with the keys 'boxes', 'whiskers', 'medians', 'caps' , but I can't get it to work when you call boxplot() directly.

However, this seems to work:

 df.plot(kind='box', color={'medians': 'blue'}, medianprops={'linestyle': '--', 'linewidth': 5}) 

see Pandas Boxplot Examples

+6
source

All Articles