gpt4 book ai didi

python - 用 Pandas 调整子图布局

转载 作者:太空狗 更新时间:2023-10-30 01:06:01 27 4
gpt4 key购买 nike

我开始意识到 ma​​tplotlibtight_layout() 不能应用于 pandas 生成的绘图。

这是我正在运行的代码:

            0         1         2         3         4
A 0.039895 0.960105 NaN NaN NaN
D 0.030418 0.969582 NaN NaN NaN
E 0.037345 0.962655 NaN NaN NaN
F 0.061522 0.938478 NaN NaN NaN
G 0.047163 0.952837 NaN NaN NaN
H 0.026423 0.000000 0.000000 0.973577 NaN

df.T.plot(kind='bar', subplots=True, width=0.7, legend=False,
layout=(2,4), sharex=True, sharey=True)
plt.tight_layout()

我最终遇到以下错误:

AttributeError: 'NoneType' object has no attribute 'is_bbox'

我也认为这与 github 上发布的类似问题有关: DataFrame.hist() does not get along with matplotlib.pyplot.tight_layout() #9351

因此,我正在寻找基于 subplots_adjust(*args, **kwargs) 的解决方法。最重要的是,我试图调整 hspace 参数。但是,调用 pandasplot 函数时不接受这些关键字参数。

有什么建议吗?

最佳答案

tight_layout() 绝对适用于 pandas!

没有 tight_layout()

df.T.plot(kind='bar', subplots=True, width=0.7, legend=False, 
layout=(3, 2), sharex=True, sharey=True)
# plt.tight_layout()

enter image description here


使用 tight_layout()

df.T.plot(kind='bar', subplots=True, width=0.7, legend=False, 
layout=(3, 2), sharex=True, sharey=True)
plt.tight_layout()

enter image description here

关于python - 用 Pandas 调整子图布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40305552/

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