Determine Matplotlib axis size in pixels


To determine the axis size in pixels, we can take the following steps −

  • Create a figure and a set of subplots, using subplots() method, fig and ax.

  • To get the DPI, use fig.dpi. Print the details.

  • Find bounding box in the display box.

  • Find the width and height, using bbox.width and bbox.height.

  • Print the width and height.

Example

from matplotlib import pyplot as plt

fig, ax = plt.subplots()
print("Dot per inch(DPI) for the figure is: ", fig.dpi)
bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width, bbox.height
print("Axis sizes are(in pixels):", width, height)

Output

Dot per inch(DPI) for the figure is: 100.0
Axis sizes are(in pixels): 4.96 3.696

Updated on: 06-May-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements