Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Legend for Contour Plot
Replies: 5   Last Post: Apr 9, 2014 6:04 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 28
Registered: 2/26/07
Re: Legend for Contour Plot
Posted: Jun 3, 2007 5:34 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Paul Brenner wrote:
> I have searched through the nathworks site and googled extensively
> but have found no simple scripts or explanations regarding how to
> automatically generate a legend for contour plots. I have toyed
> around with the clabel functionality but would much prefer a legend
> of values for each contour line color/type as opposed to placing
> the
> numerical values on or near the contour lines which makes it more
> difficult to read. Thanks for any guidance.

Hi Paul,
your question made me curious and I played with contour a little bit.
I managed to create a legend but only for a filled contour - the
reason is that the lines that are drawn are in fact the borders of
patch objects rather than actual "line" objects (which are the kind
of objects generated by plot). If one wants to get fancy, it should
be possible to take the xdata/ydata properties of such patches and
create lines, then generate a legend for them.
Anyway, this is what I did:

z = peaks(40);
zmin = floor(min(z(:))); zmax = ceil(max(z(:)));
zinc = (zmax - zmin) / 10;
zlevs = zmin:zinc:zmax; % levels
[c2,hc2] = contourf(z,zlevs); % generate contours
%(just replace contourf with contour for the non-filled case)
hcont = get(hc2,'children'); % get vector of single contours handl.
for i = 1:length(hcont)
cont_level(i) = get(hcont(i),'userdata'); % level of i-th cont.
legend_entries{i} = num2str(cont_level(i));

I hope this is of some guidance - you can refine the process to
eliminate repeated legend entries, or try and do what I suggested
before for a non-filled contour .

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.