加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – pandas DataFrame的100%面积图

发布时间:2020-12-16 23:02:41 所属栏目:Python 来源:网络整理
导读:在 pandas’ documentation中,您可以找到关于面积图的讨论,特别是堆叠它们.是否有一种简单直接的方法来获得像这样的100%区域叠加图 从this post开始? 解决方法 该方法与 the other SO answer基本相同;将每行除以行的总和: df = df.divide(df.sum(axis=1),
在 pandas’ documentation中,您可以找到关于面积图的讨论,特别是堆叠它们.是否有一种简单直接的方法来获得像这样的100%区域叠加图

从this post开始?

解决方法

该方法与 the other SO answer基本相同;将每行除以行的总和:
df = df.divide(df.sum(axis=1),axis=0)

然后你可以照常调用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()

产量

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读