如何绘制 pandas.crosstab() 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46541543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:34:21  来源:igfitidea点击:

How to plot pandas.crosstab() columns

pythonpandasmatplotlib

提问by Simone

I am trying to plot in the same figure the first nvalues which are the results of pd.crosstab(), I have tried:

我试图在同一个图中绘制作为 的结果的前n 个pd.crosstab(),我尝试过:

tab = pd.crosstab(df['genre_ids'], df['target'],margins=True).sort_values('All',ascending=False)
tab = tab.drop('All',axis=1)
tab = tab.drop('All',axis=0)
tab[:top_n].plot(kind='bar', stacked=True)
plt.show()

but I get a barplotthat doesn't rappresent the actual values present in the tab's columns.

但我得到了一个条形图,它没有显示tab列中的实际值。

EDIT:

编辑:

This is the crosstab: enter image description hereI would like plot the values in columns 0and 1, but I get this plot that it's different from the values in the columns: enter image description hereIs it possible get something like this:enter image description herebut with the 0and 1values plot in the same bar for each x-value?

这是交叉表enter image description here我想绘制第0列和第1列中的值,但我得到这个图,它与列中的值不同: enter image description here是否有可能得到这样的东西:enter image description here但是在01值中绘制每个 x 值的条形相同?

回答by Simone

Solved simply set stacked = Falseas has suggested @Nipun Batra in the comments.

stacked = False按照评论中建议的@Nipun Batra简单设置即可解决。