gpt4 book ai didi

python - 添加辅助 y 轴

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:50 24 4
gpt4 key购买 nike

我有两个图表:
enter image description here enter image description here

我合二为一:
enter image description here

这是我的代码:

df = pd.DataFrame({'a': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
'b': [3, 4, 4, 4, 5, 4, 3, 4, 4, 5],
'c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'd': [2, 3, 4, 4, 5, 5, 6, 7, 8, 10]},
index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])

df2 = pd.DataFrame({'Jan': [100,50,1/3*100,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
'Feb': [0,50,1/3*100,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
'Mar': [0,0,1/3*100,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
'Apr': [0,0,0,25,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
'May': [0,0,0,0,20,1/6*100,1/7*100,1/8*100,1/9*100,10],
'Jun': [0,0,0,0,0,1/6*100,1/7*100,1/8*100,1/9*100,10],
'Jul': [0,0,0,0,0,0,1/7*100,1/8*100,1/9*100,10],
'Aug': [0,0,0,0,0,0,0,1/8*100,1/9*100,10],
'Sep': [0,0,0,0,0,0,0,0,1/9*100,10],
'Oct': [0,0,0,0,0,0,0,0,0,10]},
index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'])

ax = df.a.plot.line()
ax2 = df2.plot.bar(stacked=True)
df.a.plot.line(ax=ax2,secondary_y=range(0,11))

我想在组合图上有一个从 0-10 的辅助 y 轴,以便组合图中的线按比例缩放到它自己绘制时的样子。我尝试使用 secondary_y 字段,但看起来我没有正确使用它。

最佳答案

您可以在 matplotlib.pyplot 中使用 ax2 = ax1.twinx() 执行此操作,然后在 ax2 上绘制堆积条形图。

关于python - 添加辅助 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379161/

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