473,408 Members | 2,477 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,408 software developers and data experts.

Minor, but annoying legend problem in matplotlib


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:
________________________________

x= [1,2,3,4,5,6,7,8]
a= [5,3,2,4,6,5,8,7]
b= [4,1,3,5,2,8,3,6]
c= [8,4,9,6,7,3,9,4]

DataSets= [a,b,c]

Symb= ['k-o','k--s','k-.^']

for index,d in enumerate(DataSets):

plot(x,DataSets[index],Symb[index])

legend(["a","b","c"])
_______________________________

This behaves just as I would want it to. Normally, though I want
'open' markers, which (AFAIK) require me to set the color to 'w'
(white), and so the (white-on-white) lines won't show up in this case
(the markers still have black outlines). I tried using two colors in
one marker definition, 'k-wo', but that didn't work.

The obvious solution is to plot the lines and symbols in two
different commands:
_______________________________

Symb= ['wo','ws','w^']
LineType= ['k-','k--','k-.']

for index,d in enumerate(DataSets):

plot(x,DataSets[index],LineType[index])
plot(x,DataSets[index],Symb[index])

legend(["a","b","c"])
_______________________________

This produces the correct plot, but the legend here alternates
between symbol and marker in its what uses for designating each dataset
(a uses 'marker a', b uses 'line b', c uses 'marker c').
Is there some rationale for this being the default behavior?

The workaround I've found has been to use two separate loops for the
symbol and line plotting:

_______________________________
Symb= ['wo','ws','w^']
LineType= ['k-','k--','k-.']

#Loop 1
for index,d in enumerate(DataSets):

plot(x,DataSets[index],LineType[index])

# Loop 2
for index,d in enumerate(DataSets):

plot(x,DataSets[index],Symb[index])

legend(["a","b","c"])
_______________________________

This works and will give me a legend that uses only marker symbols in
it, as desired. It's not an ideal solution though as I often have some
moderate amount of processing within the loop that I'd rather not have
to repeat or write out to some temporary variable just in order to have
it available for the second loop.
I've gotten around this before for somewhat similar cases using
suggestions from this group of explicitly defining the values the legend
will use:

L1= plot(x,y,...

but I can't figure how to do this here because of the looping over the
data sets.

On a related note, is there any way to increase the size of the
markers within the legend?

TIA,

J.S.

--
Actual e-mail: <Firstname> 'dot' <Lastname> @comcast.net
Jul 18 '05 #1
1 3152
>>>>> "Jorl" == Jorl Shefner <v4***@yahoo.com> writes:

Jorl> The obvious solution is to plot the lines and symbols in
Jorl> two different commands: _______________________________
You want to explicitly pass the lines you want to legend into the
legend command, as in

Symb= ['wo','ws','w^']
LineType= ['k-','k--','k-.']

leglines = []
for index,d in enumerate(DataSets):

plot(x,DataSets[index],LineType[index])
lines = plot(x,DataSets[index],Symb[index])
leglines.extend(lines)

legend(leglines, ["a","b","c"])

Jorl> to have it available for the second loop. I've gotten
Jorl> around this before for somewhat similar cases using
Jorl> suggestions from this group of explicitly defining the
Jorl> values the legend will use:

Jorl> L1= plot(x,y,...

Jorl> but I can't figure how to do this here because of the
Jorl> looping over the data sets.

Hope the above example helps here.

Jorl> On a related note, is there any way to increase the size
Jorl> of the markers within the legend?

You can access the lines of the legend instance

leg = legend(lines, labels)
lines = leg.get_lines()
set(lines, markersize=10, markeredgewidth=2) # etc

See http://matplotlib.sourceforge.net/ex...legend_demo.py

JDH
Jul 18 '05 #2

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

Similar topics

3
by: John Hunter | last post by:
matplotlib is a 2D plotting library for python. You can use matplotlib interactively from a python shell or IDE, or embed it in GUI applications (WX, GTK, and Tkinter). matplotlib supports many...
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...
3
by: gurkesaft | last post by:
Hello, Does anyone know how to use scipy.plt? The documentation is wrong or out of date at the scipy website, and I can't join their mailing lists for some reason. Most importantly, I need...
2
by: bwaha | last post by:
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...
2
by: Grant Edwards | last post by:
I downloaded examples/contour_demo.py, and it doesn't run. I've searched both the user guide and the Wiki for "contour" and got zero hits. ...
3
by: vajratkarviraj | last post by:
i hav python2.5, matplotlib0.90.1, and py2exe for python 2.5 all on windows xp... i hav a python program(letsc.py) which uses the matplotlib package... and i want 2 make an exe of it for distribution...
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....
0
by: PamMish1982 | last post by:
Hi all, I have recently started using Python and I am trying to make a GUI out of Tkinter. I am using matplotlib for the graphic purposes. I have to make a exe file from this code. I use py2exe...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.