FAQ
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.

Search Discussions

  • John Hunter at Dec 16, 2004 at 4:52 pm
    "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.

    JDH

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 16, '04 at 4:42p
activeDec 16, '04 at 4:52p
posts2
users2
websitepython.org

2 users in discussion

John Hunter: 1 post Jorl Shefner: 1 post

People

Translate

site design / logo © 2022 Grokbase