【发布时间】:2017-06-11 06:01:14
【问题描述】:
我想让 matplotlib 中的 tight_layout() 函数忽略注释。例如,这是一个带有tight_layout 的图:
fig = plt.figure()
fig.patch.set_facecolor('grey')
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)
def example_plot(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_ylabel('y-label', fontsize=fontsize)
ax.set_title('Title', fontsize=fontsize)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
plt.tight_layout()
plt.annotate('a)', xy=(0, 1), xycoords='figure fraction', ha='left', va='top')
plt.annotate('b)', xy=(0, 0.5), xycoords='figure fraction', ha='left', va='top')
plt.annotate('c)', xy=(0.5, 1), xycoords='figure fraction', ha='left', va='top')
该图的大小似乎增加了(向左和顶部),以便为注释腾出空间(ylabel 不会一直到边缘)。但是,我希望将注释添加到图形顶部而不进行任何重新排列。我可以让tight_layout 忽略这些注释吗?
编辑: 事实证明这实际上不是tight_layout 的问题。即使没有调用tight_layout,也会发生这种情节调整:
但是,如果我在另一台计算机上尝试相同的操作,则会发生预期的行为(不调整大小)。两个系统都运行 matplotlib 2.0.0 和 ipython。
【问题讨论】:
-
不清楚你在问什么。带注释和不带注释的图完全一样。这实际上通过查看代码就很清楚了,
tight_layout在添加注释之前被调用,因此它甚至不知道它们是否存在。 -
您的评论让我意识到,
tight_layout不是问题所在。也没有调整大小。令人困惑的是,在另一台具有相同版本 matplotlib 的计算机上,我得到了您描述的行为。
标签: python matplotlib