Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latex Greek fonts not working in legend #7028

Closed
mdepitta opened this issue Sep 5, 2016 · 6 comments
Closed

Latex Greek fonts not working in legend #7028

mdepitta opened this issue Sep 5, 2016 · 6 comments

Comments

@mdepitta
Copy link

mdepitta commented Sep 5, 2016

  • Fedora 20; Python 2.7.5; Matplotlib 1.4.3 (installed by yum/pip)

For some reason, I can render Latex fonts including Greek ones in my axis labels but when I try to do it with my legends in the same figure, I cannot.

Enforcing explicitly Latex as described here won't solve the issue.

In my code:

fig,ax = plt.suplot(111)
ax.plot(x,y)
ax.set_ylabel(r'$\xi$ etc.')    # Rendered correctly

# Add Legend
labels_legend = {'js'   : '$W_S,U_A$',
                 'xi'   : r'$\xi$',
                 'taup' : r'$\tau_p$'
                 }
obj_legend  = []
colors = {'js'   : 'k',
          'xi'   : 'r',
          'taup' : 'y'
         }
for k,lbl in labels_legend.iteritems():
    # Add artist for legend
    obj_legend.append(ax.plot([],[],color=colors[k], marker=" ", ls='-', lw=lw, label=labels_legend[k])[0])
# Add Legend (NOT RENDERED CORRECTLY!!!)
legend = ax.legend(obj_legend,labels_legend,numpoints=1,fontsize=legfs,frameon=True,loc=1)
@jenshnielsen
Copy link
Member

jenshnielsen commented Sep 5, 2016

Your example seems needlessly complicated. You are setting the labels twice both in the plot function and then by supplying the labels_legend to ax.legend. It looks like this is suppling the keys and not the values of the dict to the legend function.

The following works for me:

fig,ax = plt.subplots(1,1)
ax.set_ylabel(r'$\xi$ etc.')   

# Add Legend
labels_legend = {'js'   : '$W_S,U_A$',
                 'xi'   : r'$\xi$',
                 'taup' : r'$\tau_p$'
                 }
obj_legend  = []
colors = {'js'   : 'k',
          'xi'   : 'r',
          'taup' : 'y'
         }
for k,lbl in labels_legend.items():
    # Add artist for legend
    ax.plot([],[],color=colors[k], marker=" ", ls='-', lw=1, label=labels_legend[k])
legend = ax.legend(numpoints=1,fontsize=12,frameon=True,loc=1)

@mdepitta
Copy link
Author

mdepitta commented Sep 5, 2016

Sincerely thanks.
And sorry about the trivial question after all: yet sometimes you are so much into the code that you miss basic things...

Your code works.

I guess the overtly complicated code stemmed from older versions of it where I was trying to achieve a specific order in showing the legend entries. In this case, if I want the entries to appear from top-down as specified in labels_legend I just needed to control the order of entries in the for loop, i.e.

for k in ['js','xi','taup']:
    ... 

@jenshnielsen
Copy link
Member

Yes the legend will appear in the order that it is added via the plot so you can do it like that.

You can also use an ordered dict instead of a regular dictionary to store your names in the order you want. https://docs.python.org/2/library/collections.html#collections.OrderedDict

@jenshnielsen
Copy link
Member

I think this issue can be closed?

@mdepitta mdepitta closed this as completed Sep 5, 2016
@mdepitta
Copy link
Author

mdepitta commented Sep 5, 2016

Hi Jens,
yes sorry.
I am going to close it rn.

Cheers,


Maurizio De Pitta'
https://sites.google.com/site/mauriziodepitta/home

On Mon, Sep 5, 2016 at 2:04 PM, Jens Hedegaard Nielsen <
notifications@github.com> wrote:

I think this issue can be closed?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#7028 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AIBSFOdMt-YTPXRaKXAy6GhjznRIauv7ks5qnGfVgaJpZM4J1FQ8
.

@jenshnielsen
Copy link
Member

Thanks good that your problem is solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants