6

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?

5
  • It looks like fig.set_figsize(30, 1, forward=True) works, but the forward=True keyword argument is necessary. If forward=False, or if left off, it doesn't work. It's a workaround I suppose: I can do fig.set_figsize(fig.get_width(), value) to set only one. But this is a workaround, I'd like to know why it doesn't work. Maybe I should file as a bug? Commented Aug 5, 2015 at 19:49
  • Does fig = plt.figure(figsize=(30,1)) work? Commented Aug 5, 2015 at 19:52
  • @N1B4 Yes, but I want to change the figure size after creation at the time of adding a colorbar. Basically, I want to programmatically expand the window when I create a colorbar so my figure still fits nicely inside the window, and I don't know at figure creation whether I will add a colorbar or not. Commented Aug 5, 2015 at 19:54
  • You could set the figure size at the end with fig.set_size_inches(30,1) and replace 30 or 1 with something new if a colorbar is included. Or more dynamically, first determine the figure size (e.g. size = fig.get_size_inches()) and adjust that as needed. Commented Aug 5, 2015 at 19:59
  • Whoops... I meant fig.set_size_inches... not set_figsize. Commented Aug 5, 2015 at 20:00

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

2 Comments

it doesn't changed for me at all
This is what I was looking for, It worked. I adjusted height to meet my requirements as well. fig.set_size_inches(5, 15, forward=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.