matplotlib legend problem

B

bwaha

Has anyone figured out how to get a legend for each line in a
matplotlib.collections.LineCollection instance?

No problem if I plot lines the default way ie. line,=plot(x,y). But I've had
to resort to using LineCollections for the significant speed boost since I
am routinely plotting up to ten 30000 point datasets in one plot. The legend
doc says that "legend by itself will try and build a legend using the label
property of the lines/patches/collections". But it seems that for a
collection the legend refers to the collection as a whole, not the
individual lines within the collection.

AFAIK this is a limitation of matplotlib 0.85. Any ideas on how I can
workaround this problem that won't kill my speed boost?
 
B

bwaha

bwaha said:
Has anyone figured out how to get a legend for each line in a
matplotlib.collections.LineCollection instance?
After frigging around for hours I finally tracked down the real cause of the
plotting speed problem which led me to use LineCollections in the first
place. Its the 'best' option in the legend location and setting it to
default in my application!

When I first put in the LineCollection code I cut out my legend code to keep
things simple. And sure enough LineCollections plotted really fast. Then,
since legends didn't work for LineCollection lines individually I figured
I'd fudge it by creating dummy lines from the collection, adding labels and
calling legend(). This worked with only a small speed penalty. But I kept
getting a stackdump when I added the location argument. Finally realised it
was due to having a default of 'best' location in my code which meant it
went searching for intersection with lines that don't exist (outside of the
LineCollection). So I disabled the 'best' location option. Then I figured,
since I'd cleaned up my code a bit, I'd reinstate my earlier pylab.plot
based line drawing code to see if the clean up made any difference to what
was previously abysmal performance. The lines plotted faster than the
LineCollection code! When I removed the legend hack for LineCollections
there was virtually no difference. (Story is not finshed yet). So I figured
after all that that I'd reinstate my pylab.plot based code since I could
plot a greater range of symbols than with LineCollections with no speed
loss. And I thought why not go the whole hog and reinstate the 'best'
location option too. Boom! Plotting performance was abysmal again. Finally I
realised that enabling 'best' and having it as the default meant that as I
added new data to plot, the search time for a good place to put the legend
increased dramtically, and probably became more difficult with more and more
lines filling the canvas.

Anyway now I'm a lot happier than when I started because I've retained my
original range of plot styles and I got much faster plotting. Hopefully this
lesson can help someone else.
 
J

John Hunter

bwaha> added the location argument. Finally realised it was due to
bwaha> having a default of 'best' location in my code which meant
bwaha> it went searching for intersection with lines that don't
bwaha> exist (outside of the LineCollection). So I disabled the
bwaha> 'best' location option. Then I figured, since I'd cleaned
bwaha> up my code a bit, I'd reinstate my earlier pylab.plot based
bwaha> line drawing code to see if the clean up made any
bwaha> difference to what was previously abysmal performance. The
bwaha> lines plotted faster than the LineCollection code! When I
bwaha> removed the legend hack for LineCollections there was
bwaha> virtually no difference. (Story is not finshed yet). So I
bwaha> figured after all that that I'd reinstate my pylab.plot
bwaha> based code since I could plot a greater range of symbols
bwaha> than with LineCollections with no speed loss. And I thought
bwaha> why not go the whole hog and reinstate the 'best' location
bwaha> option too. Boom! Plotting performance was abysmal
bwaha> again. Finally I realised that enabling 'best' and having
bwaha> it as the default meant that as I added new data to plot,
bwaha> the search time for a good place to put the legend
bwaha> increased dramtically, and probably became more difficult
bwaha> with more and more lines filling the canvas.

bwaha> Anyway now I'm a lot happier than when I started because
bwaha> I've retained my original range of plot styles and I got
bwaha> much faster plotting. Hopefully this lesson can help
bwaha> someone else.


Sorry you had to find this out after so much trial and error. For a
small number of large length lines (eg 10 at 30k points each), plot
will be much faster than line collections as you observed. For a
large number of small lines (eg 1000 at 20 points each) line
collections will be much faster. And yes, the "best" optimization for
the legend is slow -- I'm on the fence as to whether this should be
the default or not. At least clearly flagging this as a performance
bottleneck in the docs would be useful.

Cheers,
JDH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top