![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/442583/image.jpeg)
Obtain seperate legend for each contour line
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mehdi Jaiem
am 3 Dez. 2020
Bearbeitet: Mehdi Jaiem
am 3 Dez. 2020
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 Kommentare
Akzeptierte Antwort
dpb
am 3 Dez. 2020
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.
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Contour Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!