gpt4 book ai didi

python - 从 Pandas 数据框中绘制堆积条形图

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:12 26 4
gpt4 key购买 nike

我有数据框:

payout_df.head(10)

enter image description here

复制以下 excel 图的最简单、最智能和最快的方法是什么?

enter image description here

我尝试了不同的方法,但无法将所有内容都安排妥当。

谢谢

最佳答案

如果您只想要一个堆积条形图,那么一种方法是使用循环绘制数据框中的每一列,并只跟踪累积总和,然后将其作为 bottom 传递pyplot.bar

的参数
import pandas as pd
import matplotlib.pyplot as plt

# If it's not already a datetime
payout_df['payout'] = pd.to_datetime(payout_df.payout)

cumval=0
fig = plt.figure(figsize=(12,8))
for col in payout_df.columns[~payout_df.columns.isin(['payout'])]:
plt.bar(payout_df.payout, payout_df[col], bottom=cumval, label=col)
cumval = cumval+payout_df[col]

_ = plt.xticks(rotation=30)
_ = plt.legend(fontsize=18)

enter image description here

关于python - 从 Pandas 数据框中绘制堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49889398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com