473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

matplotlib legend problem

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?
Jan 27 '06 #1
2 3637

"bwaha" <cf*****@bigpond.net.au> wrote in message
news:7w*******************@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.


Jan 27 '06 #2
>>>>> "bwaha" == bwaha <cf*****@bigpond.net.au> writes:
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
Jan 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Hunter | last post by:
matplotlib is a 2D plotting package for python with a matlab compatible syntax and output tested under linux and windows platforms. matplotlib-0.30 is available for download at...
1
by: Jorl Shefner | last post by:
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...
1
by: Jorl Shefner | last post by:
I have a problem that I run into a lot with the 'legend' command's default behavior. I've found a work-around but I wonder if there's a better way. For a simple example, take the following:...
8
by: Andi Clemens | last post by:
Hi, everytime I try to plot a bar with matplotlib I get the following error message: Traceback (most recent call last): File "bar_stacked.py", line 13, in ? p1 = bar(ind, menMeans, width,...
3
by: baber | last post by:
Question1: #col1 col2 col3 col4 171.4 130.5 7.2245646 0.3933173 98.2 133.1 6.8370089 -0.4387156 4.8 12.8 2.9705532 -1.4150375 225.1 193.5 7.705306 0.2182325 Columns are...
4
by: Bill Jackson | last post by:
Hi, I'm having some trouble plotting with the following matplotlibrc: text.usetex : True I tried clearing the cache files under ~/.matplotlib, but this did not help the problem. I'd post...
4
by: John Henry | last post by:
Has anybody been able to create an exe of their python applications involving matplotlib using pyinstall (ver 1.3)? I am getting a: RuntimeError: Could not find the matplotlib data files when...
0
by: =?ISO-8859-1?Q?Fr=E9d=E9ric_Degraeve?= | last post by:
Hi everybody, I've got a problem concerning matplotlib/pylab. I use it to represent curves. I will use these pictures in a report. However, it writes me a string 'date' on the bottom of my x-axis....
2
by: Durand | last post by:
I got a really annoying problem with datetime graphs. The problem is that with a long range time graph, the text on the x axis keeps overlapping like here:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.