Francesco Montesano, on 2011-01-21 15:44,  wrote:
> Dear All,
> 
> I am using contour plot and I am drawing different contours with
> different colors and linestyles and I would like to have a legend with
> a caption for each contour function used.
> Here you can see an example of what I would like to do
> 
> >> #create the 2D histogram and the x and y axis
> >> x, y = np.random.normal(0, 0.5, 1000), np.random.normal(0, 1, 1000)
> >> h, xe,ye = np.histogram2d(x,y, bins=25)
> >> xe, ye = (xe[1:]+xe[:-1])/2, (ye[1:]+ye[:-1])/2
> >>
> >> lines,text = [], []   # initialise lists
> >>
> >> #contour plots
> >> lines.append(plt.contour(xe,ye,h, levels=[10,9], linestyles="-", 
> >> colors="k"))
> >> text.append("level=10, 9")
> >>
> >> lines.append(plt.contour(xe,ye,h, levels=[5,4], linestyles="--", 
> >> colors="r"))
> >> text.append("level=5, 4")
> >>
> >> plt.legend(lines, text)
> 
> Everything goes well untill I plot the legend. At the end of the mail
> I report the error that I get.
> Anyway, if I do
> >> plt.legend(lines)
> I don't get any errors but it's quite useless, since the text of the
> legend is just like:
> <matplotlib.contour.ContourSet instance at 0x6bedc20>
> as you can see from the attached figure.
> 
> 
> I've the feeling that the problem is that "contour" gives back a
> "matplotlib.contour.ContourSet instance", while the functions like
> "plot" gives back a "<matplotlib.lines.Line2D object".
> 
> Does anyone knows how to do what I want?
> 
Hi Francesco,

here's one way of getting what you want, instead of calling
legend on your 'lines' variable as you had it, do this:

  actual_lines = [cs.collections[0] for cs in lines]
  plt.legend(actual_lines, text)

As you note, the call to plt.countour does not return lines, it
returns contour sets (which is why I called the variable 'cs' in
my example). Poking around in ipython, I saw that each contour
set has a collections attribute which holds the actual lines.
 
hope that helps,
-- 
Paul Ivanov
314 address only used for lists,  off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7 

Attachment: signature.asc
Description: Digital signature

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to