gpt4 book ai didi

python - Tight_Layout : Attribute Error . 'AxesSubplot' 对象没有属性 'tight_layout'

转载 作者:行者123 更新时间:2023-12-01 12:09:22 47 4
gpt4 key购买 nike

我想绘制一个堆叠条形图,其中的数据来自 Pandas 中的两个独立数据框(My_means 和 My_stds)。我正在使用 Python 3.7。

绘图调用工作正常,但是,标签被切断。设置 tiny_layout 对我没有帮助,并生成“属性错误:'AxesSubplot' 对象没有属性 'tight_layout'”。这是什么意思,我该如何克服?

我没有发现与此错误相关的任何问题,所以我认为我在做一些相当愚蠢的事情......

这是我的代码:

My_means= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.mean)
My_stds= ratios.pivot_table('Data', 'Sample ID', 'User ID', aggfunc= np.std)
plot_it = My_means.plot(kind='bar', color=stack_cols, stacked=True, yerr=My_stds, capsize=3, title='Just some graph')
plot_it.tight_layout()

谢谢你的帮助!
爵士乐

最佳答案

My_means.plot(…)返回一个轴对象。而tight_layout需要一个图形对象。您可以使用多种不同的方法:

也许最简单的方法是使用 plt.tight_layout()适用于当前数字:

import matplotlib.pyplot as plt
# Your plotting code here
plt.tight_layout()

或者,您可以预先创建图形和轴,将轴作为参数传递给 plot然后使用 tight_layout在图形对象上:
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plot_it = My_means.plot(kind='bar', ax=ax, …)

fig.tight_layout()

关于python - Tight_Layout : Attribute Error . 'AxesSubplot' 对象没有属性 'tight_layout',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53391086/

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