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!
@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']).
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With