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:
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:
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.