简体   繁体   中英

Pandas - Plot stacked bar chart

I have a pandas dataframe representing a table with 2 columns and 4 rows

Name | Value
 n1  | 10.3
 n2  | 5
 n3  | 15
 n4  | 8

I need to draw stacked bar (matplotlib) of values and the names should be shown in legend I tried this (with no success):

df.count().unstack('Total_Sales')
df.plot(kind='bar', stacked=True)

How can I do it? I saw this question Pandas - Plotting a stacked Bar Chart . Not sure how to use it in my case

If you want to use the names as categories you need to have them in the columns. So first set the index to 'Name' and then transpose.

df.set_index('Name').T.plot(kind='bar', stacked=True)

堆积的情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM