0

Here is a MWE with a legend being produced:

import matplotlib.pyplot as plt
from matplotlib import rcParams

fig, ax = plt.subplots()
ax.plot( [1,2], [2,1], label='\$f_\mathrm{so}\$')
ax.legend(loc='upper right')
#fig.show()

rcParams['svg.fonttype'] = 'none'
fig.savefig('fig.svg', format='svg')

This is what the resulting fig.svg looks like:

produced output

Now I'm looking for a way to tell matplotlib to use less horizontal space for the legend text, opting for a figure that looks like this:

desired output

I studied the documentation of the legend API but didn't find a solution.
Is it actually not possible to influence the amount of space being used for the legend text?

Background: I intend to include the svg file in a LaTeX documents via the svg-package such that the LaTeX commands are properly typeset. I would also have the ticklabels converted to something like \btick{1.0}, \rtick{1.0} etc.

Update

There is a workaround proposed here answering the question Fix size of legend in matplotlib. The proposed remedy involves tailoring the bbox_to_anchor to the desired width of the legend, provided relative to the width of the axis.

In the context of the question Fix size of legend in matplotlib that is fine, but I need a solution that enables me to set the absolute width of the legend, such that it does not resize when changing the width of the axis.

Bastian
  • 901
  • 7
  • 23
  • 3
    Does this answer your question? [Fix size of legend in matplotlib](https://stackoverflow.com/questions/41026125/fix-size-of-legend-in-matplotlib) – Jay Patel Feb 17 '21 at 13:44
  • It sort of does, indeed, but this approach (via bbox_to_anchor) requires me to specify the width of the bbox_to_anchor, and hence the width of the legend box relative to the width of the axis. This means that I have to adapt the call of ax.legend(...) whenever I change the width of the axis. That's quite fiddly. I would prefer a means of simply providing the absolute width of the legend box and have the bbox_to_anchor untouched. – Bastian Feb 17 '21 at 13:57

0 Answers0