user11305439
user11305439

Reputation: 117

PANDAS. Grouping dataframe items and stack plot

I have a pandas dataframe as below.

enter image description here

I want to be able to plot the barchart with the same Names on the same stacked bar and colored differently like this:

enter image description here

Thanks for any help Jason

Upvotes: 0

Views: 56

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Not totally colored as you want, but you can use this as a start:

df = pd.DataFrame({'Name':['R1','T1','Y1','R1','T2','T1'],
                  'volume':[10,15,12,12,5,2]})

(df.assign(c=df.groupby('Name').cumcount())
  .pivot(index='Name', columns='c',values='volume')
  .plot.bar(stacked=True)
)

Output:

enter image description here

Upvotes: 1

Related Questions