Reputation: 10203
This question is related to this one. After I plot the barplot with different colors, I would like to add a corresponding legend but I cannot figure out how. Is it even possible?
df = pd.DataFrame({'index':np.arange(0,10,1),'values':np.random.randint(0,100,10),
'group':list('aaabbbcccd')})
colors = {'a':'red','b':'gold','c':'lightblue','d':'cyan'}
df['values'].plot(kind='bar', color=[colors[i] for i in df['group']])
It doesn't seem possible in the present solution but I tried iterating over the groups(using groupby) and plotting them onto the same axis but the plots don't come out correctly. Any alternative suggestions?
Upvotes: 0
Views: 35
Reputation: 14847
You can use .pivot
to get things into the right structure and then just use .plot
:
df.pivot("index", "group")["values"].plot(kind="bar")
Upvotes: 1