Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Messages: [ Previous | Next ]
 Jonas Posts: 1 Registered: 4/9/14
Re: Legend for Contour Plot
Posted: Apr 9, 2014 6:04 AM

Thank you Claude and Jessica, this helped me a lot!

Though for me it didn't work perfectly for filled plots (contourf), so I made som changes.

%generate plot
[X,Y,Z]=peaks(20);
figure

[~,h]=contourf(X,Y,Z,-2:3);
hc=get(h,'children');
levels=zeros(length(hc),1);
for n=1:length(hc)
%'CData' works better for filled plots
levels(n)=get(hc(n),'CData');
% for regular contour plots the same procedure as suggested earlier works fine
% levels(n)=get(hc(n),'UserData');
end
% remove duplicates
[levels,I]=unique(levels);
hc=hc(I);
% sort (here in descending order)
[levels,I]=sort(levels,'descend');
hc=hc(I);
% remove NaN values that are assigned if limits are used in the plot
hc=hc(isfinite(levels));
levels=levels(isfinite(levels));
% insert the legend
legend(hc,num2str(levels))

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