Hubschr
Hubschr

Reputation: 1335

How do I set the resize-behaviour of subplots?

I'm working with several subplots in a window. Just after plotting it looks great, problem is when I rezise the windows manually. The subplots do not adapt correctly to the new size of the window. How can I fix that?

Window after plotting, looking great: enter image description here

window after resizing it manually, looks bad: enter image description here

EDIT:

A simply demo-Code:

from matplotlib.pyplot import *

figure(figsize=(24,6))

subplot(131)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")

subplot(132)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")


subplot(133)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")

tight_layout()
show()

As you see, the plot looks good after plotting it. But when you start to shrink the plot horizontally, the space between plot and plot gets smaller and smaller. And at the end, the ticklabels are on others plots, because there's no space for them. I need to know how to set that the entire plot gets smaller, leving space for the labels.

Upvotes: 3

Views: 2326

Answers (1)

Hubschr
Hubschr

Reputation: 1335

Maybe not exactly the answer of my question, but it solves my problem:

After creating the figure, you connect the resize-event to an eventhandler:
cid = fig.canvas.mpl_connect('resize_event', onresize)

def onresize(event):
    plt.tight_layout()

As Wicket said, I'm just calling tight_layout() again and again, but automatically.

Upvotes: 3

Related Questions