2

I want to make a square plot with matplotlib. That is, I want the whole figure to be square. When I use the following code, the width of the resulting image is still a bit larger than the height. Why is matplotlib not respecting the figsize I provide?

import matplotlib.pyplot as plt 
fig, ax = plt.subplots(figsize=(10, 10))

# When inspecting in browser, reveals 611x580 px image
ax.plot([1,2,3], [1,2,3])

Edit: I display the image inline in a Jupyter notebook, and just use the Chrome developer tools to inspect the image.

Evgeny
  • 4,173
  • 2
  • 19
  • 39
miroli
  • 234
  • 1
  • 7

1 Answers1

5

That is a problem of jupyter notebook. The figure it shows is a "saved" version, which uses the bbox_inches="tight" option and hence changes the size of the shown image.

One option you have is to save the figure manually to png,

fig.savefig("output.png")

As @EvgenyPogrebnyak commented, the other option is to deactivate the "tight" option in the notebook as

%matplotlib inline
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
fig, ax = plt.subplots(figsize=(10, 10))

# When inspecting in browser, 
ax.plot([1,2,3], [1,2,3])  # now reveals 720 x 720 px image

as seen in this answer.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 1
    also can try resetting the 'ticght' option in the notebook: https://stackoverflow.com/questions/26714626/how-to-disable-bbox-inches-tight-when-working-with-matplotlib-inline-in-ipytho – Evgeny Jun 19 '18 at 13:13
  • @EvgenyPogrebnyak very good point. Added this to the answer (unless you want to provide your own answer with this info?) – ImportanceOfBeingErnest Jun 19 '18 at 13:20
  • lets keep the useful information in one single answer – Evgeny Jun 19 '18 at 13:22