5

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

Zaus
  • 1,089
  • 15
  • 25
  • Hmm, would `\hspace*{1em}` work? If you aren't using transparency, could you use the "ugly dot" with white colour? https://stackoverflow.com/q/9169052/683329 – Jiří Baum Aug 18 '20 at 00:32
  • 1
    @sabik Nope, `\hspace*{1em}` (or any hspace) is not working either. – Zaus Aug 18 '20 at 00: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 Aug 18 '20 at 01:12
  • I can't test myself, but maybe try `\mbox{}\hphantom{whitespace} ugly leading dot` – samcarter_is_at_topanswers.xyz Aug 18 '20 at 08:37
  • @samcarter_is_at_topanswers.xyz Good idea, but nope, `\mbox{}` is being ignored as well... :( – Zaus Aug 18 '20 at 09:18
  • If the labels are typeset by LaTeX, how comes that they are in a different font from, say, xtick labels? I have checked a different solution, using `\strut`, and it works in LaTeX and it does not in matplotlib. Also, everything else you've tried do work in Latex, do not in Matplotlib. – gboffi Oct 14 '22 at 16:33
  • @gboffi The different font between tick labels and legend labels, comes down to default settings. I've made the MWE slightly less minimal such that now tick labels and legend labels match. Anyhow, that is unfortunately not what is causing the issue. But, yes, everything I do works in latex but not in matplotlib, which is precisely why I am asking this question... – Zaus Oct 14 '22 at 17:35
  • Likely it's Matplotlib that, somehow, trims the labels. If you raise an issue, could you please post a link in comments? Tia. – gboffi Oct 14 '22 at 19:38
  • 1
    @gboffi, issue raised on [matplotlib's GitHub page](https://github.com/matplotlib/matplotlib/issues/24166#issue-1409723459) – Zaus Oct 16 '22 at 00:33
  • Thank you for the notification, I'm curious… – gboffi Oct 16 '22 at 07:54
  • It seems one cannot add whitespace, but maybe it is possible to move the text. I managed to do it for ylabel by setting `y=0.4`. For legend texts, it should be more difficult, but maybe still possible. – piogor Apr 19 '23 at 10:05

0 Answers0