【发布时间】:2014-05-09 04:02:35
【问题描述】:
我在使用 plt.tight_layout() 尝试整理带有多个子图的 matplotlib 图时遇到了问题。
我已经创建了 6 个子图作为示例,并希望使用 tight_layout() 整理它们的重叠文本,但是我收到以下 RuntimeError。
Traceback (most recent call last):
File ".\test.py", line 37, in <module>
fig.tight_layout()
File "C:\Python34\lib\site-packages\matplotlib\figure.py", line 1606, in tight_layout
rect=rect)
File "C:\Python34\lib\site-packages\matplotlib\tight_layout.py", line 334, in get_tight_layout_figure
raise RuntimeError("")
RuntimeError
这里给出了我的代码(我使用的是 Python 3.4)。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 3*np.pi, 1000)
fig = plt.figure()
ax1 = fig.add_subplot(3, 1, 1)
ax2 = fig.add_subplot(3, 2, 3)
ax3 = fig.add_subplot(3, 2, 4)
ax4 = fig.add_subplot(3, 3, 7)
ax5 = fig.add_subplot(3, 3, 8)
ax6 = fig.add_subplot(3, 3, 9)
for ax in [ax1, ax2, ax3, ax4, ax5, ax6]:
ax.plot(x, np.sin(x))
fig.tight_layout()
plt.show()
我最初怀疑问题可能来自具有不同大小的子图,但是tight layout guide 似乎表明这不应该是一个问题。任何帮助/建议将不胜感激。
【问题讨论】:
-
致反对者:你能解释一下为什么你觉得这个问题应该被反对吗?如果您觉得它可以通过某种方式改进,请告诉我。
标签: python python-3.x matplotlib