简体   繁体   中英

Slicing plot using different colors - manually recolor the bars

I would like to slice my DF using chosen colors. I know how to slice DF but I don't know how to put all together in a one plot. Now MWE gives plots like this: 产量

 import pandas as pd
 import numpy as np

 index=pd.date_range('2011-1-1 00:00:00', '2011-1-31 23:50:00', freq='1h')
 df=pd.DataFrame(np.random.randn(len(index),2).cumsum(axis=0),columns=['A','B'],index=index)

 df2 = df.groupby([lambda x: x.month, lambda x: x.day]).sum()

 df2[:11].plot(kind='bar', color='r')
 df2[12:].plot(kind='bar', color='y')

I would like to have one plot (not two as in example) with all 31 values, where for range [:11] the plot color would be red and for [12:] yellow.

You need to concatenate each so that they are separate series. You also need to rename them so they are not exactly the same (I've appended a space).

df3 = pd.concat([df2[:11], df2[12:]], axis=1)
df3.columns = ['A', 'B', 'A ', 'B ']
df3.plot(kind='bar', colors=['r', 'r', 'y', 'y'])

Alternatively, specify the color for each value in the series.

colors = tuple(['r'] * 11 + ['y'] * (len(df2) - 11))    
df2.plot(kind='bar', color=[colors], legend=False)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM