Search All of the Math Forum:

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

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

 Messages: [ Previous | Next ]
 Claude Posts: 28 Registered: 2/26/07
Re: Legend for Contour Plot
Posted: Jun 3, 2007 5:34 PM

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));
end
legend(hcont,legend_entries)

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 .
Claude

Date Subject Author
6/2/07 Paul Brenner
6/3/07 Claude
9/22/13 Jessica Piper
4/9/14 Jonas
6/4/07 Matt Wolinsky
9/23/13 Açmae