matplotlib legend problem

bwaha cfriedl at bigpond.net.au
Fri Jan 27 11:05:31 EST 2006


"bwaha" <cfriedl at bigpond.net.au> wrote in message
news:7wmCf.228485$V7.15610 at news-server.bigpond.net.au...
> 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.







More information about the Python-list mailing list