Zoozoo
Zoozoo

Reputation: 240

plot colors by color values in pandas dataframe

I have a dataframe that looks like this.

raw_data = {'Enum': ['E330','E322','E124','E500'],'Count': [234, 122, 765, 433],
'Colors':['red','blue','green','yellow']}

additives_count = pd.DataFrame(raw_data)

I want to plot a bar graph and I used the code below.

ax = additives_count.plot(kind='barh',colors=additives_count['Colors'])

The only issue is I am not able to make the respective colors appear with the Enum. I got my bar plot, but only 1 color. So for instance, my plot should have E330 plotted as red, E322 as blue, E124 as green and E500 as yellow. How can I achieve this?

Do note that this is just a very small subset of my data. I have a total of 30 rows to plot, but i believe you get the gist of what I am trying to achieve. Any help would be much appreciated.

Thank you!

Upvotes: 6

Views: 3248

Answers (2)

Space Impact
Space Impact

Reputation: 13255

Specify which column is x-axis and which one is y-axis.

additives_count.plot(x='Enum', y='Count',kind='barh',color=additives_count['Colors'])

The output looks like this. enter image description here

Upvotes: 4

DavidG
DavidG

Reputation: 25363

Specifying the x and y data in the plotting call seems to solve the problem:

ax = additives_count.plot(x="Enum",y="Count",kind='barh',color=additives_count['Colors'])

Note that colors is being depreciated, so it is recommended to use color. This will give:

enter image description here

Upvotes: 3

Related Questions