gpt4 book ai didi

python - Pandas 与 twinx 一起绘图

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

我正在尝试使用 Pandas matplotlib API 在同一张图上绘制条形图和线图。然而,进展并不顺利。我正在使用 twinx(),这似乎是实现此目的的普遍接受的方式。

请注意,这是在 Jupyter notebook 中完成的,绘图以内联方式显示。感谢您的协助!

fig0, ax0 = matplotlib.pyplot.subplots()
ax1 = ax0.twinx()

trend_df_hours.plot(kind='bar', stacked=True, color=color_list, ax=ax0)
trend_df_qty.plot(kind='line', secondary_y=True, ax=ax1)
matplotlib.pyplot.show()
matplotlib.pyplot.close()

最佳答案

问题是什么?您的代码似乎工作正常。

%matplotlib inline
from matplotlib import pyplot as plt

trend_df_hours = pd.Series(np.random.rand(10))
trend_df_qty = pd.Series(np.random.rand(10))

fig0, ax0 = plt.subplots()
ax1 = ax0.twinx()

trend_df_hours.plot(kind='bar', stacked=True, ax=ax0)
trend_df_qty.plot(kind='line', secondary_y=True, ax=ax1)
plt.show()
plt.close()

enter image description here

关于python - Pandas 与 twinx 一起绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678435/

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