6

I would like to insert some leading whitespace into some of my legend labels (problem is the same for other types of labels) with latex turned on. However, any leading whitespace that I enter is being ignored (see MWE and image below). An ugly workaround would be to enter a leading . (inspired by this Q&A). That's not pretty, though.

Any idea how I could get this to work properly? Can it even be done, or should I look into raising an issue with matplotlib?

MWE:

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'serif'
plt.rcParams['text.usetex'] = True
plt.rcParams['mathtext.fontset'] = 'cm'

fig = plt.figure(figsize=(4, 3))
plt.plot([], label="~ tilde")
plt.plot([], label="\, comma")
plt.plot([], label="\quad quad")
plt.plot([], label="\hphantom{whitespace} hphantom")
plt.plot([], label=".\hphantom{whitespace} ugly leading dot")
plt.xlabel("\hphantom{whitespace} xlabel")
plt.legend()

leading white space in matplotlib legend with latex

11
  • Hmm, would \hspace*{1em} work? If you aren't using transparency, could you use the "ugly dot" with white colour? stackoverflow.com/q/9169052/683329 Commented Aug 18, 2020 at 0:32
  • 1
    @sabik Nope, \hspace*{1em} (or any hspace) is not working either.
    – Zaus
    Commented Aug 18, 2020 at 0:58
  • 1
    @sabik I tried fiddling with the textcolor, but using \usepackage{color} is a whole other can of worms. The suggestions about changing the textcolor from that Q&A don't work for me, they mess up line spacings.
    – Zaus
    Commented Aug 18, 2020 at 1:12
  • I can't test myself, but maybe try \mbox{}\hphantom{whitespace} ugly leading dot Commented Aug 18, 2020 at 8:37
  • 1
    @gboffi, issue raised on matplotlib's GitHub page
    – Zaus
    Commented Oct 16, 2022 at 0:33

0

Browse other questions tagged or ask your own question.