Pandas DataFrame 的 100% 面积图

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

100% area plot of a pandas DataFrame

pythonpandasplot

提问by Dror

In pandas' documentationyou can find a discussion on area plots, and in particular stacking them. Is there an easy and straightforward way to get a 100% area stack plot like this one

pandas 的文档中,您可以找到关于area plots的讨论,特别是堆叠它们。有没有一种简单直接的方法来获得像这样的 100% 面积叠加图

enter image description here

在此处输入图片说明

from this post?

这个帖子

回答by unutbu

The method is basically the same as in the other SO answer; divide each row by the sum of the row:

该方法与其他SO答案中的方法基本相同;将每一行除以该行的总和:

df = df.divide(df.sum(axis=1), axis=0)

Then you can call df.plot(kind='area', stacked=True, ...)as usual.

然后你就可以df.plot(kind='area', stacked=True, ...)像往常一样打电话了。



import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(2015)

y = np.random.randint(5, 50, (10,3))
x = np.arange(10)
df = pd.DataFrame(y, index=x)

df = df.divide(df.sum(axis=1), axis=0)
ax = df.plot(kind='area', stacked=True, title='100 % stacked area chart')

ax.set_ylabel('Percent (%)')
ax.margins(0, 0) # Set margins to avoid "whitespace"

plt.show()

yields

产量

enter image description here

在此处输入图片说明