Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 438
Zoozoo Avatar asked Mar 12 '18 09:03

Zoozoo


People also ask

How do you color a column in pandas?

@sqllearner you can apply the same color to several columns just by adding them to the subset, like df. style. set_properties(**{'background-color': 'red'}, subset=['A', 'C']).

How do you add a legend to a pandas plot?

Make a dictionary d with keys Column1 and Column2. Make a data frame using DataFrame (d). Plot the data frame with a list of styles. Using legend(), place a legend on the figure.

What is the method of DataFrame to plot a line chart?

line() function is used to plot series or DataFrame as lines. This function is useful to plot lines using DataFrame's values as coordinates. Columns to use for the horizontal axis.


2 Answers

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

like image 185
Space Impact Avatar answered Sep 20 '22 21:09

Space Impact


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

like image 41
DavidG Avatar answered Sep 20 '22 21:09

DavidG