3

So I am having a little alignment issue with my legend in matplotlib. Hopefully it is easily solvable with the right know-how. I have scoured the matplotlib website but I'm struggling to find the exact solution.

Essentially I have the following axis vertical spans with the following labels (note that I have used $\mathrm{}$ for these):

fig = plt.figure()
ax = fig.add_subplot(111)

ax.axvspan(3851.1, 3951.1, color='gray', alpha=0.4, lw=1,label='$\mathrm{D}_{n}(4000)$')
ax.axvspan(4001.1, 4101.2, color='gray', alpha=0.4, lw=1)
ax.axvspan(4084.7, 4123.4, color='gray', alpha=0.8, lw=1,label='$\mathrm{H}\delta_{\mathrm{A}}$')

I also have the following legend label:

ax.legend(prop={'size':8}, loc=2)

Now the problem that I am seeing is this (for the image I have increased the prop size to 12 to show the issue but it scales down for size 8):

enter image description here

My issue is that the alignment is slightly off between the vspan regions and the math mode descriptive labels. Taking away the math mode solves the problem, but the labels do not contain the correct subscripts and greek lettering that I require. See here:

enter image description here

I was wondering if anyone knew of any alignment arguments for this niche scenario?

GCien
  • 2,221
  • 6
  • 30
  • 56
  • 2
    Maybe you are experiencing the same issue as this: http://stackoverflow.com/questions/31690373/wrong-offset-when-using-math-mode-and-subscripts-in-matplotlib-and-osx. Try changing your backend to see if the problem still persist. – Jean-Sébastien Aug 09 '15 at 17:58

0 Answers0