Joel
Joel

Reputation: 2175

Resizing matplotlib figure with set_fig(width/height) doesn't work

For some reason this code creates a figure that is only the standard size: it doesn't change the height or width (I chose widths and heights that are ludicrous to clearly illustrate the problem):

import matplotlib.pyplot as plt

fig = plt.figure()
fig.set_figwidth(30)
fig.set_figheight(1)

print('Width: {}'.format(fig.get_figwidth()))

plt.show()

I'm running on OSX 10.10.4, Python 3.4.3, Matplotlib 1.4.3. (Installed via Macports.) Any ideas? What am I missing?

Upvotes: 6

Views: 14030

Answers (2)

River
River

Reputation: 9093

The optional parameter forward propagates changes to the canvas in a GUI window that already exists.

Documentation here:

optional kwarg forward=True will cause the canvas size to be automatically updated; e.g., you can resize the figure window from the shell

Using Figure(figsize=(w,h)) also works.

For Matplotlib 2.2.0 and newer

forward=True is the default for all three of set_figheight, set_figwidth, and set_size_inches (set_size_inches changes both height and width simultaneously).

For Matplotlib 1.5.0

forward=True must be specified explicitly as it is False by default. (In Matplotlib 2.0.0, the default is changed to True only for set_size_inches).

For Matplotlib 1.4.3 and older

forward is only supported by set_size_inches.

set_figheight and set_figwidth do not support this argument, so it is a bit difficult to only change a single dimension of a pre-created GUI.

Upvotes: 7

Joel
Joel

Reputation: 2175

I'm not certain why those documented functions don't work, but they have been fixed for the next matplotlib release (>v1.4.3). As a workaround until the next release, replacing set_figwidth and set_figheight solves this problem.

import matplotlib.pyplot as plt

fig = plt.figure()
# Instead of set_figwidth(30)
fig.set_size_inches(30, fig.get_figheight(), forward=True)

plt.show()

Upvotes: 4

Related Questions