Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


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 nonfilled 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 ith 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 nonfilled contour . Claude



