0

I've run into an issue with my matplotlib chart outputs, which I am exporting as SVG text (using fig.savefig() and rendering as server-side rendered analytics in a web app.

One of these charts is a horizontal bar chart - as pictured below in stunning detail -

                  _______________________
       item1_name |----                 |
       item2_name |--------             |
        something |----------           |
thing thing thing |-------------        |
      other thing |---------------------|
                  ---1---2---3---4---5---
                        some metric       

My problem is when these labels are very large, they are causing trouble.

When using fig.tight_layout():

                  _______________________
ry Very Long Name |----                 |
eft Side Gets Cut |--------             |
 Keep Getting Cut |----------           |
names are getting |-------------        |
   cutoff on left |---------------------|
                  ---1---2---3---4---5---
                        some metric       

Plot renders as normal but X axis labels are cut off on the left side (oddly the plot actually seems to render larger than normal in some cases)

When using rcParams.update({'figure.autolayout': True}):

                                 ________
My Very Very Very Very Long Name |-     |
                Now I Am Smushed |--    |
             Chart is very small |---   |
             but names are being |----- |
                rendered in full |------|
                                 -1----5-
                                 some metr      

Names render in full, but plot is squished to make space.

I am hoping to make it so plot is always the exact same size regardless of scale of x axis, length of y labels - while also never cut ylabels on the left.

Is there an mpl convenience method to render the whole chart & labels without distortion/cutoff? I believe tight_layout() is supposed to do this but only works for xlabels.

If not, is there a method to get the size required for the ylabel, which I could then use with fig.set_size_inches(), like so:

label_inches_needed = get_label_size(axes)
plot_consistent_height = 4
plot_consistent_width = 10
fig.set_size_inches(plot_consistent_width + label_inches_needed, plot_consistent_height)

Appreciate any suggestions and thank you for looking at my charts.

Reference - Why is my xlabel cut off in my matplotlib plot?

8t12c7081
  • 683
  • 3
  • 9
  • 30

1 Answers1

2

To make sure labels are not cut out, try using the option bbox when saving. I can't verify for your case since you did not provide code for your figure but this usually does the trick.

plt.savefig(figname, bbox_inches = "tight")
BossaNova
  • 1,509
  • 1
  • 13
  • 17