简体   繁体   English

熊猫 - 情节堆积条形图

[英]Pandas - Plot stacked bar chart

I have a pandas dataframe representing a table with 2 columns and 4 rows 我有一个pandas数据帧表示一个包含2列和4行的表

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): 我需要绘制值的堆叠条(matplotlib),名称应该在图例中显示我试过这个(没有成功):

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 . 我看到了这个问题Pandas - 绘制一个堆积的条形图 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. 首先将索引设置为'Name'然后转置。

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

堆积的情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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