HandleVisibility using the fit function

15 次查看(过去 30 天)
I am fitting a surface to three dimensional data as follows:
f=fit([x,y],z,'poly22');
I want to plot the x,y,z data points as well as the surface. I am doing that as follows:
plot(f,[x,y],z);
However, I only want the legend to be for the data points, not the fitted surface. I can't figure out where to put the
'HandleVisibility','off'
so that the surface does not show up in the legend. Everywhere I try to put it, I get an error.
Any suggestions?
Thanks.

回答(3 个)

Sean de Wolski
Sean de Wolski 2011-12-27
So you only want the legend to apply to some parts of the plot? Try feeding the handles you want the legend to contain into legend:
figure; hold on
H(1) = plot(1:10,1:10,'b-');
H(2) = plot(1:10,(1:10)+50,'k-');
H(3) = plot(1:10,(1:10).^2,'r-');
legend(H([1 3]),'1:10','(1:10).^2')

Robert
Robert 2011-12-27
Well, the problem is that
plot(f,[x,y],z);
Plots both the fitted surface f and the x,y,z data simultaneously. I can't figure out how to isolate the fitted surface so that it doesn't show up in the legend.
Maybe a better way of asking this question is how can I plot the surface and the x,y,z data independently of one another?

Walter Roberson
Walter Roberson 2011-12-27
I believe you probably want something like
plot([x,y],f,[x,y],z);
Otherwise you are feeding an odd number of inputs to plot(), which would cause one of the pairs of data to use sequential coordinates rather than the coordinate system you have established.
Instead of working with handle visibility to control legends, you should be working with IconDisplayStyle. See http://www.mathworks.com/help/techdoc/creating_plots/braliom.html#bram7nu
When plot()'ing x-y pairs, if y is a row vector, then 1 lineseries object will be generated, and otherwise as the number of lineseries objects will be the same as the number of columns in y. If you assign the output of plot() to a variable to get the handles, this calculation allows you to figure out how many of the handles relate to each of the x-y pairs.

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by