MATLAB Answers


add label to a contour when when multiples plots

Asked by Beatriz Sanchez on 18 Sep 2019 at 17:48
Latest activity Commented on by Star Strider
on 19 Sep 2019 at 19:19
Hello saviors,
so, I'm creating three contour from trhee matrix array on a single plot using hold on:
hold on
contour (muvec, muvec, spaln(:,:,1))
contour (muvec, muvec, spaln(:,:,2))
contour (muvec, muvec, spaln(:,:,3))
hold off
What I want is to label each contour with 1, 2, 3 for each matrix, so I can know which contour represent wich matrix array.
thank you all


Sign in to comment.




1 Answer

Answer by Star Strider
on 18 Sep 2019 at 18:02
 Accepted Answer

They appear to be plotted on the same set of coordinate matrices. One option is to use different line colours (and other variations) for each contour plot. You might also consider contour3, that will allow you to separate them vertically.


Show 1 older comment
My pleasure.
I would use different colours, and label them in the legend.
As I mentioned, you can also plot the contour outputs as planes with constant offsets between the planes. You would have to get the (x,y) data from each call to the contour function for that, and add your own z vectors to to each one offset them.
Example Code —
M1 = peaks;
[X,Y] = meshgrid((1:49)*pi/25);
M2 = sin(X) .* cos(Y);
[H1,C1] = contour(M1);
L1 = C1.LevelList;
Lv1 = ismember(H1(1,:), L1);
H1p = H1;
H1p(:,Lv1) = NaN;
[H2,C2] = contour(M2);
L2 = C2.LevelList;
Lv2 = ismember(H2(1,:), L2);
H2p = H2;
H2p(:,Lv2) = NaN;
plot3(H1p(1,:), H1p(2,:), ones(size(H1p(1,:)))*1)
hold on
plot3(H2p(1,:), H2p(2,:), ones(size(H2p(1,:)))*4)
hold off
grid on
zlim([0 5])
text([0; 0], [0; 0], [1; 4], {'Contour 1','Contour 2'})
Example Plot —
add label to a contour when when multiples plots - 2019 09 18.png
Experiment to get the result you want.
thank you, I didn't do it exactly like you, but your way help me a lot!
As always, my pleasure!

Sign in to comment.