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 ]
Jonas

Posts: 1
Registered: 4/9/14
Re: Legend for Contour Plot
Posted: Apr 9, 2014 6:04 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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))



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.