Legend problems in MatPlotLib
jdhunter at ace.bsd.uchicago.edu
Thu Dec 16 17:52:10 CET 2004
>>>>> "Jorl" == Jorl Shefner <v451v at 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.
More information about the Python-list