1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

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

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    My setup:


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

      ...
      ax2.plot(x, y2)

      plt.tight_layout()
      plt.show()

    • adding keypress handler (so I would just press "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:

      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.

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

    Continue reading...

Compartilhe esta Página