![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/442583/image.jpeg)
Obtain seperate legend for each contour line
21 次查看(过去 30 天)
显示 更早的评论
Hello everyone
%% sin(t)-(1/(1.5*V));
figure
[t,V]=meshgrid(-5:0.33:5,-5:0.3:5);
slope=sin(t)-(V./1.5);
length=sqrt(1+slope.^2);
quiver(t,V,1./length,slope./length,0.79);
xlabel('t in s')
ylabel('V(t)')
box on
title('slope field equation (2)')
legend('slope field')
yticks([-5:5:5]);
xticks([-6:2:6]);
axis([-6 6 -5 5])
hold on
c=[-2:1:2];
[C,h]=contour('v6',t,V,slope,c);
Here is my code but it displays the legend as in figure 1. I want the legend to be like in figure 2.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/442433/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/442438/image.png)
The legend in fi1 is placed in data1. I don't want to do it like that. I want every line to have its own legend
UPDATE!!!!!!!!!!!!!!!!: found the solution here
https://de.mathworks.com/matlabcentral/answers/284500-contour-plot-legend-how-to-change-symbol-to-straight-line
0 个评论
采纳的回答
dpb
2020-12-3
There's no way to make legend use the contour lines directly; there are not any object handles to them; and the legend is global to the object as you observe.
Again, one has to revert to subterfuge --
...
hQ=quiver(t,V,1./length,slope./length,0.79); % save handle
...
[C,hC]=contour('v6',t,V,slope,c); % identify handle
i1=2:C(2,1)+1:size(C,2); % retrieve beginning of contour lines
clr={'r','o','m','g','c'}; % define some colors -- not very distinct, could do better
hL=arrayfun(@(i,c) plot(C(1,i:i+60),C(2,i:i+60), ...
['-' c{:}],'DisplayName',num2str(C(1,i-1),'incline %d V/s')),i1,clr); % draw the contour lines
hLg=legend([hQ hL]); % legend for only objects wanted
produced:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/442583/image.jpeg)
Would seem worthy of enhancement request to contour altho I have serious doubts would ever be addressed...they think you're supposed to label the contour lines themselves instead.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!