1

How do I get the size (width and height) of the rectangle of a plot create with matplotlib's pyplot library. Specifically I need the width of the box: enter image description here

Here is a part of the code:

import matplotlib.pyplot as plt                                                           
plt.figure()                                                  
bar_plot = plt.bar(df.index, df_mean, yerr=df_std*1.96, color=colors);
Tahlil
  • 1,031
  • 1
  • 14
  • 26
  • 1
    Have you tried `fig.get_size_inches()*fig.dpi` ? – Smectic May 29 '20 at 17:00
  • @Smectic : It seems you copied the answer from the duplicate I marked. In such cases, you should have also included what `fig` is i.e., `fig = plt.figure()` – Sheldore May 29 '20 at 17:01
  • I use it recently but I don't know is the output give the size with or without the labels. But your link gave me the answer. Thx – Smectic May 29 '20 at 17:04
  • @Sheldore I think it returns the entire figure size not the rectangle box's size, right? – Tahlil May 29 '20 at 17:15
  • 1
    @Tahlil I check a plot having `array([314., 192.])` as the output of `fig.get_size_inches()*fig.dpi` and measured it using Inkscape. The value correspond to the entire size of the plot not the box – Smectic Jun 02 '20 at 12:28

1 Answers1

0

You can get the with of the box using

ax.get_window_extent().transformed(ax.get_figure().dpi_scale_trans.inverted()).width*ax.get_figure().dpi
dukeeloo
  • 161
  • 7