controling matplotlib's figure size - aspect ratio seems wrong

Viewed 45

I wish to create a small square figure - 1x1 inch.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=[1,1])
plt.show()

If I evaluate fig, it returns <Figure size 100x100 with 0 Axes>. However, on the screen, a rectangular figure is displayed:

enter image description here

How do I generate a figure with a square canvas? And why the code above generates this elongated canvas, isn't figsize defined in inches? What am I missing?

(This is Matplotlib 3.5.3, TkAgg, on Ubuntu)

EDIT: Bigger square figures (e.g., 3x3 inch) are displayed correctly as squared figures on the screen. However, it seems that matplotlib applies some heuristic that makes the figure at least 2.7 inches wide.

EDIT 2: A related GitHub issue, no working solution: https://github.com/matplotlib/matplotlib/issues/2716

1 Answers

Found an inelegant solution - switch to a backend that doesn't have this bug.

import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
fig = plt.figure(figsize=[1,1])
plt.show()

This results with a correct aspect ratio:

enter image description here

(requires installing PyQt5 - pip install PyQt5)

Related