1

I'm working with matplotlib pylab backend_inline

enter image description here

This code set figure and Axes in different colors

fig, ax = plt.subplots()
ax.set_facecolor('xkcd:salmon')
fig.patch.set_facecolor('k')

and plot this figure

enter image description here

so that it's easy to measure the length for the figure and Axes.

I estimated the length and got Axes/figure is equal to about 90%.

However, a so post gives this code

bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width, bbox.height

to get width and height of the Axes.

and I got this value

(4.6499999999999995, 3.0199999999999996)

fig.get_size_inches() gives array([6., 4.])

this code gives the same result

bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted())

which results in a ratio of 77.5%, which is quite different from the ratio I measured.

However, when I saved the figure to a file,

plt.savefig("ax_fig_ratio")

I got this

enter image description here

which has the expected ratio.

How do I get the actual size of the figure and Axes for the first figure rendered inline?

How do I make matplotlib inline_backend render the figure in width of 6 inches and Axes in 4.65 inches, as fig.get_size_inches() indicates?

According to @JodyKlymak, the figure size is trimmed. If it is true, how do I disable the trimming feature so that I can get the full-size figure?

JJJohn
  • 915
  • 8
  • 26
  • try this:`bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted())` ax is the graph frame and fig is the background frame of the graph. – r-beginners Jun 20 '21 at 14:14
  • How did you make the first figure? If it was with bbox_inches= tight or in the Jupyter notebook with the inline back end, the figure size is trimmed to remove extra white space which your initial call to get_window_extents doesn’t know anything about. – Jody Klymak Jun 20 '21 at 15:27
  • https://stackoverflow.com/questions/26714626/how-to-disable-bbox-inches-tight-when-working-with-matplotlib-inline-in-ipytho – Jody Klymak Jun 21 '21 at 14:47

0 Answers0