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

标签 python matplotlib layout label attributeerror

我想绘制一个堆叠条形图,其中的数据来自 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/

相关文章:

python - 在pyspark中检索每组DataFrame中的前n个

python - 作为 Snakefile 输出的函数或字典理解

python - 如何在 python 中创建我自己的映射类型

python - Pandas/matplotlib 将 2018 年和 2019 年显示为 48 和 49

java - Java布局中的元素都在同一位置

python - Keras 掩蔽层作为 lstm 层的输入

python - 更改x轴的刻度

python - 为什么绘图函数 plt.show() 在循环内部或外部时表现不同?

Android View GONE 如果语言

android - 可以在自定义 Progressbar progressDrawable 中做圆角吗?