pandas 使用熊猫数据帧数据python创建堆叠直方图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24594511/
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-13 22:13:51  来源:igfitidea点击:

creating stacked histogram with pandas dataframes data python

pythonmatplotlibpandasgraphing

提问by ccsv

I am trying to create a stacked histogram with data from 2 or more uneven pandas dataframes? So far I can get them to graph on top of each other but not stack.

我正在尝试使用来自 2 个或更多不均匀 Pandas 数据帧的数据创建堆叠直方图?到目前为止,我可以让它们在彼此之上绘制图形,但不能堆叠。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('dert.csv', encoding = "ISO-8859-1", index_col=0)

df1['text'] = df['text'].dropna(subset=['five'])
df2['printed'] = df['text2']
ax = df1['text'].hist( bins=100, range=(1,100), stacked=True, color = 'r')
ax = df2['printed'].hist(bins=100, range=(1,100), stacked=True, color = 'g')

plt.setp(ax.get_xticklabels(),  rotation=45)
plt.show()

How do I get them to stack?

我如何让它们堆叠?

I found a solution but it does not use pandas dataframes Matplotlib, creating stacked histogram from three unequal length arrays

我找到了一个解决方案,但它不使用Pandas数据帧Matplotlib,从三个不等长的数组创建堆叠直方图

回答by Happy001

The method in that post should work:

该帖子中的方法应该有效:

plt.hist([df1['text'],df2['printed']],
          bins=100, range=(1,100), stacked=True, color = ['r','g'])