7

I'm trying to create figures in matplotlib that read nicely in a journal article. I have some larger figures (with subfigures) that I'd like to take up nearly an entire page in portrait mode (specifically, 6.5"x9" for a full-page figure with 1" margins on US letter paper). I can set the figure size easily with the figsize parameter. However, the figure is compressed if I set the figure size to be larger than my screen size (I'm working on a 13" laptop); specifically, the height is an issue. The dimensions of the saved figure do not change as long as the height parameter below is larger than the height of my screen:

height = 9
fig, ax = plt.subplots(3, 2, figsize=(6.5, height))
plt.savefig('test.png') # size of this figure is independent of height
                        # if height > height of my screen

How can I make matplotlib use the requested figure size even when it exceeds my screen's dimensions? I'm using spyder.

Pete
  • 504
  • 4
  • 15
  • 1
    Might not be the ideal solution, but can you save as an SVG file, and then open in something like Inkscape and export as an appropriate quality PNG file? – Andrew Guy Apr 08 '19 at 04:25
  • @AndrewGuy just tried `height=9` and `height=10` with `test.svg` instead of `test.png` - the resulting files were identical. – Pete Apr 08 '19 at 05:01
  • What backend are you using? Run `import matplotlib; matplotlib.get_backend()`. – Andrew Guy Apr 08 '19 at 05:11
  • I'm using Qt5Agg – Pete Apr 08 '19 at 05:12
  • I can't reproduce this issue running the same code within a Jupyter notebook, so I suspect it is down to what backend you are using to draw the figure. Maybe try another backend? – Andrew Guy Apr 08 '19 at 05:16
  • 1
    For example, run `import matplotlib; matplotlib.use('Agg')` at the start of your script. – Andrew Guy Apr 08 '19 at 05:18
  • That works, thanks @AndrewGuy! – Pete Apr 08 '19 at 05:29
  • I'll add it as an answer. :) – Andrew Guy Apr 08 '19 at 05:32
  • 2
    The problem is the use of spyder here. If you run the code as script from the CLI, it'll work as expected. Spyder turns on interactive mode by default and therefore the figure is "shown" before `savefig` is called. Upon showing, it is resized to match the screen. Using `agg` backend is one solution, because that cannot show anything. Turning off "matplotlib support" in the spyder options is another option. Also, possibly just adding `plt.ioff()` might work (not tested). – ImportanceOfBeingErnest Apr 08 '19 at 11:11

1 Answers1

3

This is likely an issue with your backend for plot generation.

You can see what backend you are using by running:

import matplotlib; matplotlib.get_backend()

Try changing the backend to something else, for example:

import matplotlib

matplotlib.use('Agg')

Note that this has to be run before importing matplotlib.pyplot.

Andrew Guy
  • 9,310
  • 3
  • 28
  • 40
  • 2
    Does not work for me. I get `UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show()` – Forklift17 Dec 21 '20 at 19:04
  • @Forklift17 Yes, this is expected behaviour if you are trying to show the plot inline rather than save to a file. See this question for more details - https://stackoverflow.com/questions/4930524/how-can-i-set-the-backend-in-matplotlib-in-python – Andrew Guy Dec 23 '20 at 12:24
  • If I understand right, if I use figure size that does not fit into the computer screen there is no way whatsoever to see this figure on the screen with right proportions? – Pygmalion Sep 07 '22 at 17:32