Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

x axes in plot bar visualization in matplotlib

i have df.
df

I want add in my visualization ‘Sub-Category’ in x axes.
How i can do that?
Code:

df_neg_val.plot(kind='bar',x = 'Year',y='Profit')

plot bar

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If i pointing two args in x = ” – i have error.
I’m read documentation, but i’m bad know english 🙁
Thanks for you reply 🙂

>Solution :

You could .pivot() your DataFrame so that Year becomes the index and SubCategory the columns, then plot it.

df = pd.DataFrame({
   "Year": [2000, 2000, 2001, 2001, 2002, 2002],
   "Pet": ["Dog", "Cat", "Dog", "Cat", "Dog", "Cat"],
   "Weight": [10, 5, 11, 6, 10, 6]})
df.pivot(index="Year", columns="Pet", values="Weight").plot.bar()

Bar plot example

As an aside, I don’t love bar plots with years along the x-axis, as I don’t consider years to be a categorical variable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading