'x axes in plot bar visualization in matplotlib [duplicate]

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

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 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1