By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Legend problems in MatPlotLib

P: n/a
I've only been able to plot data with both symbols and lines by
issuing two plot commands, one for markers and one for lines. That's
perfectly fine, but it creates a problem when I try to create a legend
for it. For some reason, the legend command by default alternates
between using symbols and lines, grabbing displaying the symbol from the
first plot command for the first series, then displaying a line type
from the second plot for the next series, etc.
This behavior is a bit strange, and in fact unacceptable when the
same line type is used for each series.

For instance, assume I plot four series using these markers: square,
circle, x and +, and then plotted a solid line to connect each series of
markers. The legend would describe the four series using: square, solid
line, x, solid line. Clearly, that wont' work.

I've been able get around this previously by issuing the legend
command right after the first plot command, before the second plot
command. In my current usage, though, I'm looping through a number of
files that are being plotted on the same graph. In this case, the fact
that I've plotted lines in the preceding loop causes the legend command
to revert to this alternating behavior.

Is there some way to explicitly define the symbols used by the legend
command? Or some way to plot lines with markers in the same command
which would presumably avoid this problem?

In general, plotting with matplotlib is so easy and intuitive that
I'm surprised to be having so much difficulty doing something as simple
as this. I'm sure I must be missing something obvious. The
documentation of the legend command was unenlightening.

Thanks,

J.S.
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
>>>>> "Jorl" == Jorl Shefner <v4***@yahoo.com> writes:

Jorl> I've only been able to plot data with both symbols and
Jorl> lines by issuing two plot commands, one for markers and one
Jorl> for lines. That's perfectly fine, but it creates a problem
Jorl> when I try to create a legend for it. For some reason, the
Jorl> legend command by default alternates between using symbols
Jorl> and lines, grabbing displaying the symbol from the first
Jorl> plot command for the first series, then displaying a line
Jorl> type from the second plot for the next series, etc. This
Jorl> behavior is a bit strange, and in fact unacceptable when the
Jorl> same line type is used for each series.

Example code always helps, but note you can plot a line with lines and
markers like this

plot(x, y, '--s') # dashed line with square markers.

In this case matplotlib will recognize this as one line and should do
the legending properly.

However, if you do, which I infer you are from your post

plot(x, y, 's') # square markers.
plot(x, y, '--s') # square markers.

the plot will look the same but matplotlib considers this to be two
different lines and the legend will be messed up.

Note you can explicitly control what gets added to the legend, eg with

l1, l2 = plot(x, y, '--s', x1, y1, 'go')
p = bar(x3,y2)
legend((l1, p), ('My lines', 'My bars'))

In other words, if you don't like the way the autolegend is setup, you
can explicitly control the process.

Hope this helps.

JDH

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.