I'm creating multiple subplots (bar charts) from pandas dataframe. I would like the two bars in each plot to have different colours (so that one is always eg. orange, and the other always blue).
My original dataframe looks like that:
Triplet sequence 3 4
0 AAA 78.415603 98.189959
1 AAC 85.606777 98.792146
2 AAG 85.229351 97.165199
3 AAT 78.397072 99.430332
4 ACA 85.413897 98.212358
.. ... ... ...
59 TGT 85.516804 98.094537
60 TTA 78.197088 99.040233
61 TTC 85.347600 98.243830
62 TTG 85.546867 98.017210
63 TTT 78.257015 99.585926
Then, I'm using a transpose to get this:
df2 = df.set_index('Triplet sequence').T
[64 rows x 3 columns]
Triplet sequence AAA AAC AAG AAT ACA \
3 78.415603 85.606777 85.229351 78.397072 85.413897
4 98.189959 98.792146 97.165199 99.430332 98.212358
Eventually, my code for barcharts is:
bar = df2.plot(kind='bar' ,
stacked=False,figsize = (20,40),
title='Grouped Bar Graph with dataframe',
subplots = True, layout=(16,4), legend = False, sharey = True ,
fontsize = 5, ylim = (95,100), color=[['C0','C1']] )
Then I get an error: Invalid color ['C0', 'C1']
. How can I specify color parameter so I get different color for each bar representing a row in my dataframe?
Thank you!
CodePudding user response:
I took the data you posted and updated it a little so that there are some rows that have both columns with values > 95. My plotted data looks like this.
df2 = df.set_index('Triplet sequence').T
df2
>>
Triplet sequence AAA AAC AAG AAT ACA TGT TTA TTC TTG TTT
3 98.415603 85.606777 85.229351 98.397072 96.413897 85.516804 98.197088 85.34760 99.546867 78.257015
4 98.189959 98.792146 79.165199 99.430332 98.212358 98.094537 79.040233 98.24383 98.017210 99.585926
Adding colors within df2.plt()
may not be the way to get each of the bars in different colors. You would need to update the bars in each graph to show the right color sequence. Your code has been modified and same is below...
bar = df2.plot(kind='bar' ,
stacked=False,figsize = (20,20),
title='Grouped Bar Graph with dataframe',
subplots = True, layout=(5,4), legend = False, sharey = True ,
fontsize = 5, ylim = (95,100))
for graph_row in bar: # Gets you a row of subplots
for grph in graph_row : #Gets you the graph
for ptch, color in zip(grph.patches, ["tab:orange", "tab:blue"]): #The bar
ptch.set_facecolor(color)
will you you the below bar. Note that you can change the tab:orange
and tab:blue
to any other color. Also, I am assuming that each graph has just two bars. If 3, the sequence will be orange-blue-orange and so on.
The output graph is as below.