Reputation: 8568
I am able to get very nice colorful bar plots when I use matplotlibs commands. This was very helpful which uses some coloring commands like
for box in bp['boxes']:
# change outline color
box.set( color='#7570b3', linewidth=2)
# change fill color
box.set( facecolor = '#1b9e77' )
However, the problem in using these boxplot commands, data should be grouped already as you want on the x-axis. My last question on S.O. was on that and as suggested in the reply, I can just use
df.boxplot('data1',by='data2')
This works out great for me as I can use my data as it is. However, I cannot use some coloring options as above here. In fact, parameter color
is also not accepted as an argument like as shown in several examples like this
df.plot(kind='box',color='red')
but I still prefer using the df.boxplot
commands (may be I need to learn more on other options).
Clearly, I am new to python and even newer to plotting tools so I do not understand how each of these options work in depth but would like to get insights on what is the quickest way of making a bar plot and adding colors to it.
SO my question is how do I add colors to my box plot if I use df.boxplot
Edit: Adding working example to show what I am looking for
if I have a dataframe as this
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
df=pd.DataFrame(np.random.rand(10,2),columns=['data1','data2'])
df['group']=['A','B','C','A','B','C','A','B','C','A']
and I am using following command to draw a box plot where data is grouped by column group
and I want to see the spread of column `data1' in that.
plt.figure(3)
df.boxplot(column='data1',by='group')
How do I add colors to it?
I know how to add colors if I use this command
plt.figure(1)
data2plot=[df['data1'],df['data2']]
plt.boxplot(data2plot)
This does not give the plot which I want and to get the plot which I want, I really have to use the following code:
grA=[val for ind, val in zip(df['group'],df['data1']) if ind=='A']
grB=[val for ind, val in zip(df['group'],df['data1']) if ind=='B']
grC=[val for ind, val in zip(df['group'],df['data1']) if ind=='C']
plt.figure(2)
data2plot_new=[grA,grB,grC]
plt.boxplot(data2plot_new)
I know how to add colors if I use the above method but it does not look efficient.
Another option I know where 'color' parameter can be used is this
df.plot(data2plot,kind='box',color='red')
but again even here I need to do some data transformation to get the plot I want.
So again the question is how do I change plot properties like color, thickness etc. if i use the first option.
I hope this is clear.
Upvotes: 1
Views: 540
Reputation: 8568
More complete answer from @Goyo should have been
box1=df.boxplot('data1',by='group',return_type='dict',patch_artist=False)
[x.set(color='g',linewidth=2) for x in box1['data1']['boxes']]
[x.set(facecolor='r') for x in box1['data1']['boxes']]
(@Goyo, while your answer surely helped me in digging for more information, it was interesting that you were complaining about an incomplete question from me and then posted an answer which was incomplete! ).
I am posting this as an answer because it serves my current purpose.
Upvotes: 1
Reputation: 12610
Use return_type='dict'
.
df.boxplot(column='data1',by='group', return_type='dict')
OrderedDict([('data1',
{'boxes': [<matplotlib.lines.Line2D at 0x9986e70>,
<matplotlib.lines.Line2D at 0x999e2f0>,
<matplotlib.lines.Line2D at 0x99ab9d0>],
'caps': [<matplotlib.lines.Line2D at 0x99926b0>,
<matplotlib.lines.Line2D at 0x99929f0>,
<matplotlib.lines.Line2D at 0x999ed90>,
<matplotlib.lines.Line2D at 0x99ab0f0>,
<matplotlib.lines.Line2D at 0x9a6d470>,
<matplotlib.lines.Line2D at 0x9a6d7b0>],
'fliers': [<matplotlib.lines.Line2D at 0x9992ff0>,
<matplotlib.lines.Line2D at 0x99ab770>,
<matplotlib.lines.Line2D at 0x9a6de30>],
'means': [],
'medians': [<matplotlib.lines.Line2D at 0x9992d10>,
<matplotlib.lines.Line2D at 0x99ab410>,
<matplotlib.lines.Line2D at 0x9a6dad0>],
'whiskers': [<matplotlib.lines.Line2D at 0x9986fd0>,
<matplotlib.lines.Line2D at 0x9992370>,
<matplotlib.lines.Line2D at 0x999e710>,
<matplotlib.lines.Line2D at 0x999ea50>,
<matplotlib.lines.Line2D at 0x99abdd0>,
<matplotlib.lines.Line2D at 0x9a6d130>]})])
Upvotes: 0