legend: changing the text colour

Hi,

I am plotting several different symbols using 3 different colours. The
colours indicate different data sets, whereas the symbols need not be
explained. I would therefore like each label to have a different colour,
i.e. each line in my legend should be written in a different colour
specified. The legend is getting too long if I have to indicate what each
symbol represents, plus it would be a repetition of the 3 data sets in
question. How can I change the colour of the text in the legend?

Second, how can I change the marker in the legend? I am plotting using
errorbar(), but the marker shows up as a dot, and I would like it to show up
as a '+', without having to change the actual dots in the plot.

Here is a snippet of my code:

import matplotlib as mpl
import matplotlib.pyplot as plt

fig = plt.figure(); ax = []
for k in range(1,4):
    ax.append(fig.add_subplot(3,1,k))
    for [data,col,leg] in
[[data1,'k','set1'],[data2,'r','set2'],[data3,'b','both']]:
       
ax[-1].errorbar(data[:,2],data[:,4],xerr=data[:,3],yerr=data[:,5],fmt='.',color=col,label=leg)
        ax[-1].plot(x,y,'-',color=col,label=leg)
        lgd=ax[-1].legend(loc='lower right')
        #this is what I tried to change the symbols in the legend, but it
also changes the plot
        #symbols and I would like to avoid that:
        plt.setp(lgd.get_lines(), marker='+')

I have searched this forum, other forums, and google, without finding an
answer to my questions. If there is another post or webpage already dealing
with these problems I apologise for posting them here too and ask you to
please direct me to the right pages.

Cheers, Karianne

···

--
View this message in context: http://old.nabble.com/legend%3A-changing-the-text-colour-tp29614647p29614647.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,

I am plotting several different symbols using 3 different colours. The
colours indicate different data sets, whereas the symbols need not be
explained. I would therefore like each label to have a different colour,
i.e. each line in my legend should be written in a different colour
specified. The legend is getting too long if I have to indicate what each
symbol represents, plus it would be a repetition of the 3 data sets in
question. How can I change the colour of the text in the legend?

Do something like

l1, = plot([1,2,3])
leg = legend([l1], ["Test"])

leg_texts = leg.get_texts() # list of matplotlib Text instances.
leg_texts[0].set_color("b")

Second, how can I change the marker in the legend? I am plotting using
errorbar(), but the marker shows up as a dot, and I would like it to show up
as a '+', without having to change the actual dots in the plot.

I think it is best to use a proxy artist.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

For example,

col, leg = "b", "test"
errorbar([1,2,3], [1,2,1],xerr=[0.1, 0.1, 0.1], yerr=[0.1, 0.1, 0.1],
         fmt='.',color=col)
l2, = plot(,, "+", color=col)
l2.remove() # remove from the axes

legend([l2], [leg])

IHTH,

-JJ

ps. A code snippet, that cannot be run standalone, is not very useful.
If you do not want to post your own data, use some fake data.

···

On Fri, Sep 3, 2010 at 11:04 PM, karianne <karianne@...1721...> wrote:

Thank you, JJ, this solves my problems.

I have one question to your reply:

Jae-Joon Lee wrote:

col, leg = "b", "test"
errorbar([1,2,3], [1,2,1],xerr=[0.1, 0.1, 0.1], yerr=[0.1, 0.1, 0.1],
         fmt='.',color=col)
l2, = plot(,, "+", color=col)
l2.remove() # remove from the axes

legend([l2], [leg])

Does it make a difference whether I remove l2 from the axes or not? I can't
see that it is plotting anything at all so I am curious as to what I am
missing here..

Cheers, Karianne

···

--
View this message in context: http://old.nabble.com/legend%3A-changing-the-text-colour-tp29614647p29632842.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Thank you, JJ, this solves my problems.

I have one question to your reply:

Jae-Joon Lee wrote:

col, leg = "b", "test"
errorbar([1,2,3], [1,2,1],xerr=[0.1, 0.1, 0.1], yerr=[0.1, 0.1, 0.1],
         fmt='.',color=col)
l2, = plot(,, "+", color=col)
l2.remove() # remove from the axes

legend([l2], [leg])

Does it make a difference whether I remove l2 from the axes or not? I can't
see that it is plotting anything at all so I am curious as to what I am
missing here..

Cheers, Karianne

···

--
View this message in context: http://old.nabble.com/legend%3A-changing-the-text-colour-tp29614647p29632843.html
Sent from the matplotlib - users mailing list archive at Nabble.com.