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);
5
  • 1
    Have you tried fig.get_size_inches()*fig.dpi ? Commented May 29, 2020 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() Commented May 29, 2020 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 Commented May 29, 2020 at 17:04
  • @Sheldore I think it returns the entire figure size not the rectangle box's size, right? Commented May 29, 2020 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 Commented Jun 2, 2020 at 12:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.