如何在 show() 之后为 matplotlib 图设置 tight_layout
How to set tight_layout for matplotlib graphs after show()
我的设置:
Debian Linux 8.3 amd64,XMonad WM,python2.7,matplotlib 1.5.1
问题:
我在作图,例如:
import matplotlib.pyplot as plt
x = xrange(10)
y1 = [i ** 2 for i in x]
y2 = [1.0 / (i + 1) for i in x]
fig = plt.figure()
ax1 = plt.subplot(1, 2, 1)
ax1.plot(x, y1)
ax2 = plt.subplot(1, 2, 2)
ax2.plot(x, y2)
plt.show()
并且因为我使用的是平铺 window 管理器,所以 matplotlib 的 window 得到
拉伸成瓷砖。不幸的是,这会使图表变小和布局
有点松。
如果我想"tighten"一点点,点击“配置子图->紧
Layout" 就可以了。但这意味着我必须先点击图标,然后再点击
按钮 每次 因为我用这个图来显示测试数据
运行 经常这样,很烦人。所以我尝试了一些东西
这更容易:
我尝试了什么:
在 show()
之前调用 plt.tight_layout()
:
...
ax2.plot(x, y2)
plt.tight_layout()
plt.show()
添加按键处理程序(所以我只需按 "t"):
...
ax2.plot(x, y2)
def kbd_handler(event):
if event.key == 't':
plt.tight_layout()
plt.gcf().canvas.mpl_connect('key_press_event', kbd_handler)
plt.show()
调用 tight_layout
图:
ax2.plot(x, y2)
fig.tight_layout()
plt.show()
一切都没有改变,或者至少一行中的 2 个子图看起来是一样的,
然而,对于不止一行,它使布局更加松散,所有子图都非常小。
我怀疑的是:
我认为问题与调整大小有关,这可能发生在
window 已创建,因此 tighten_layout
与原始 window 一起使用
方面。当 Window 管理器调整 window 的大小时,布局保持
子图大小,我有一个带有微型图的 "loose" 布局..
问题
有没有办法自动(或者很容易 - 比如使用 key_press_event)
收紧 布局,即使 window 在之后立即调整大小
呼叫 plt.show()
?
你可以在Matplotlib event handling的帮助下实现你想要的。只需编写一个调用 tight_layout
并在每次调整图形大小时更新图形的小函数:
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0,np.pi,100)
y1 = np.sin(x)
y2 = np.cos(x)
fig, (ax1,ax2) = plt.subplots(nrows=1, ncols=2)
fig.tight_layout()
ax1.plot(x,y1)
ax2.plot(x,y2)
def on_resize(event):
fig.tight_layout()
fig.canvas.draw()
cid = fig.canvas.mpl_connect('resize_event', on_resize)
plt.show()
当然,这与系统或版本相关的可能性很小。我在 MacOS Sierra 上用 Python 3.5.4 (Matplotlib 2.0.2) 和 Python 2.7.14 (Matplotlib 2.1.0) 验证了上面的例子。希望这有帮助。
编辑:
事实上,我认为如果您在 tight_layout
之后更新数字,我认为您的按键处理程序会起作用,即如果您调用 fig.canvas.draw()
。
我的设置:
Debian Linux 8.3 amd64,XMonad WM,python2.7,matplotlib 1.5.1
问题:
我在作图,例如:
import matplotlib.pyplot as plt
x = xrange(10)
y1 = [i ** 2 for i in x]
y2 = [1.0 / (i + 1) for i in x]
fig = plt.figure()
ax1 = plt.subplot(1, 2, 1)
ax1.plot(x, y1)
ax2 = plt.subplot(1, 2, 2)
ax2.plot(x, y2)
plt.show()
并且因为我使用的是平铺 window 管理器,所以 matplotlib 的 window 得到 拉伸成瓷砖。不幸的是,这会使图表变小和布局 有点松。
如果我想"tighten"一点点,点击“配置子图->紧 Layout" 就可以了。但这意味着我必须先点击图标,然后再点击 按钮 每次 因为我用这个图来显示测试数据 运行 经常这样,很烦人。所以我尝试了一些东西 这更容易:
我尝试了什么:
在
show()
之前调用plt.tight_layout()
:... ax2.plot(x, y2) plt.tight_layout() plt.show()
添加按键处理程序(所以我只需按 "t"):
... ax2.plot(x, y2) def kbd_handler(event): if event.key == 't': plt.tight_layout() plt.gcf().canvas.mpl_connect('key_press_event', kbd_handler) plt.show()
调用
tight_layout
图:ax2.plot(x, y2) fig.tight_layout() plt.show()
一切都没有改变,或者至少一行中的 2 个子图看起来是一样的, 然而,对于不止一行,它使布局更加松散,所有子图都非常小。
我怀疑的是:
我认为问题与调整大小有关,这可能发生在
window 已创建,因此 tighten_layout
与原始 window 一起使用
方面。当 Window 管理器调整 window 的大小时,布局保持
子图大小,我有一个带有微型图的 "loose" 布局..
问题
有没有办法自动(或者很容易 - 比如使用 key_press_event)
收紧 布局,即使 window 在之后立即调整大小
呼叫 plt.show()
?
你可以在Matplotlib event handling的帮助下实现你想要的。只需编写一个调用 tight_layout
并在每次调整图形大小时更新图形的小函数:
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0,np.pi,100)
y1 = np.sin(x)
y2 = np.cos(x)
fig, (ax1,ax2) = plt.subplots(nrows=1, ncols=2)
fig.tight_layout()
ax1.plot(x,y1)
ax2.plot(x,y2)
def on_resize(event):
fig.tight_layout()
fig.canvas.draw()
cid = fig.canvas.mpl_connect('resize_event', on_resize)
plt.show()
当然,这与系统或版本相关的可能性很小。我在 MacOS Sierra 上用 Python 3.5.4 (Matplotlib 2.0.2) 和 Python 2.7.14 (Matplotlib 2.1.0) 验证了上面的例子。希望这有帮助。
编辑:
事实上,我认为如果您在 tight_layout
之后更新数字,我认为您的按键处理程序会起作用,即如果您调用 fig.canvas.draw()
。