简体   繁体   English

如何在show()之后为matplotlib图设置tight_layout

[英]How to set tight_layout for matplotlib graphs after show()

My setup: 我的设置:

Debian Linux 8.3 amd64, XMonad WM, python2.7, matplotlib 1.5.1 Debian Linux 8.3 amd64,XMonad WM,python2.7,matplotlib 1.5.1

Problem: 问题:

I'm making a plot, for example: 我正在制作一个情节,例如:

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()

and since I'm using tiling window manager, the matplotlib's window gets stretched to a tile. 因为我正在使用平铺窗口管理器,所以matplotlib的窗口被拉伸到一个平铺。 Unfortunately this makes the graphs small and layout somewhat loose. 不幸的是,这使图形变小,布局有些松散。

If I want to "tighten" it a litte bit, clicking "Configure subplots->Tight Layout" does the job. 如果我想“收紧”它,请点击“配置子图 - >紧密布局”完成工作。 But this means I have to click on the icon and then on the button every time and since I use this plot to display testing data and run it very often, it's quite annoying. 但这意味着我必须每次点击图标然后点击按钮 ,因为我使用这个图来显示测试数据并且经常运行它,这非常烦人。 So I tried a few things to make this easier: 所以我尝试了一些方法来使这更容易:

What have I tried: 我试过了什么:

  • calling plt.tight_layout() before show() : show()之前调用plt.tight_layout() show()

     ... ax2.plot(x, y2) plt.tight_layout() plt.show() 
  • adding keypress handler (so I would just press "t"): 添加按键处理程序(所以我只按“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() 
  • calling tight_layout on figure: 在图上调用tight_layout

     ax2.plot(x, y2) fig.tight_layout() plt.show() 

It all changed nothing or at least it looked the same for 2 subplots in one row, however for more than one row, it made the layout even more loose and all subplots extremely small. 这一切都没有改变,或者至少对于一行中的2个子图看起来相同,但是对于多行,它使布局更松散,所有子图都非常小。

What I suspect: 我怀疑的是:

I believe the problem is related to resize which probably happens after the window is created, so the tighten_layout works with original window dimensions. 我认为问题与调整大小有关,这可能发生在窗口创建后,因此tighten_layout与原始窗口尺寸一起使用。 When the Window Manager resizes the window, the layout keeps the subplot sizes and I have a "loose" layout with miniature graphs.. 当窗口管理器调整窗口大小时,布局保留子图块大小,并且我有一个带有微型图形的“松散”布局。

The question 这个问题

Is there a way to automaticaly (or very easily - like using a key_press_event) tighten the layout even when the window gets resized immediately after calling plt.show() ? 有没有办法自动(或非常容易 - 像使用key_press_event) 收紧布局,即使在调用plt.show()后立即调整窗口大小?

You can achieve what you want with the help of Matplotlib event handling . 借助Matplotlib事件处理,您可以实现您想要的效果 Just write a small function that calls tight_layout and updates the figure every time the figure is resized: 只需编写一个调用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()

There is of course a small chance that this is system or version dependent. 当然,这很可能是系统或版本依赖的。 I verified the above example on MacOS Sierra with Python 3.5.4 (Matplotlib 2.0.2) and with Python 2.7.14 (Matplotlib 2.1.0). 我在MacOS Sierra上用Python 3.5.4(Matplotlib 2.0.2)和Python 2.7.14(Matplotlib 2.1.0)验证了上面的例子。 Hope this helps. 希望这可以帮助。

EDIT : 编辑

In fact, I think your keypress handler would have worked if you would have updated the figure after tight_layout , ie if you would have called fig.canvas.draw() . 事实上,如果你想在tight_layout之后更新数字,我认为你的按键处理程序会tight_layout ,即如果你已经调用了fig.canvas.draw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM