Create a specific legend in Matlab

1 view (last 30 days)
Wissem-Eddine Khatla
Wissem-Eddine Khatla on 11 Apr 2021
Commented: Cris LaPierre on 12 Apr 2021
Hello everyone,
I have put together some experimental data that I have plotted in a rather rudimentary way using Matlab.
Here is the code in question:
%% TEST pince de 2.5 cm
y = [7.58 7.12 6.55 6.20 5.60 5.45 5.18 5.02 4.96 4.75 4.55 4.41]; % Longueur d'onde
x = [0.0755 0.0943 0.1022 0.1030 0.1187 0.1297 0.1430 0.1506 0.1634 0.1808 0.1898 0.1930]*1.5625; % Fréquence de rotation
z = [9 10 11 12 13 14 15 16 17 18 19 20]; % Nombre de volts
h = zeros(6,1);
h(1) = plot(x,y,'b')
h(2) = plot(x,y,'-o')
ylabel('\lambda (cm)')
xlabel('\Omega (Hz)')
hold on
y = [7.39 7.12 6.44 6.04 5.63 5.40 5.10 4.96 4.80 4.50 4.33]; % Longueur d'onde
x = [0.0668 0.0755 0.0943 0.1022 0.1086 0.1254 0.1349 0.1460 0.1573 0.1660 0.1828]*1.5625; % Fréquence de rotation
z = [8 9 10 11 12 13 14 15 16 17 18]; % Nombre de volts
h(3) = plot(x,y,'k')
h(5) = plot(x,y,'-o')
ylabel('\lambda (cm)')
xlabel('\Omega (Hz)')
y = [7.39 6.58 5.92 5.74 5.27 5.04 4.83 4.73 4.28 4.23]; % Longueur d'onde
x = [0.0605 0.0777 0.0856 0.0938 0.1082 0.1193 0.1300 0.1466 0.1506 0.1680]*1.5625; % Fréquence de rotation
z = [8 9 10 11 12 13 14 15 16 17]; % Nombre de volts
h(4) = plot(x,y,'r')
h(6) = plot(x,y,'-o')
ylabel('\lambda (cm)')
xlabel('\Omega R^{2}')
title('Mesure ruban rose largeur 1.5 cm avec une pince de 2.5 cm de largeur')
legend(h,'7 cm','pince de 2.5 cm','10.5 cm','12.5 cm');
grid on
hold off
%%
What I am trying to do in practice: you will note that there are 3 sets of (x,y) which each correspond to an experiment carried out for a precise experimental length. For each pair of plots (x,y) it is essential to associate a colour that allows the length in question to be distinguished in the legend (so each pair (x,y) would correspond to a colour which itself corresponds to a given length). But in addition: the three (x,y) pairs were made for another characteristic length which this time is common to them, so to notify this I wanted to draw each curve with a distinct colour but with a unique marker (typically a circle for the three pairs).
Unfortunately, I can't translate what I want into a clean and understandable legend so I'm asking for your help if possible,
I thank you in advance for your assistance--

Answers (1)

Cris LaPierre
Cris LaPierre on 11 Apr 2021
I don't follow your description completely. What should the actual legend be? Should there be 6 items in your legend? Three using lines of different color, and 3 with diferent line colors + a circle marker?
Here's what does that.
%% TEST pince de 2.5 cm
y = [7.58 7.12 6.55 6.20 5.60 5.45 5.18 5.02 4.96 4.75 4.55 4.41]; % Longueur d'onde
x = [0.0755 0.0943 0.1022 0.1030 0.1187 0.1297 0.1430 0.1506 0.1634 0.1808 0.1898 0.1930]*1.5625; % Fréquence de rotation
plot(x,y,'b','DisplayName','Name1')
hold on
plot(x,y,'-o','DisplayName','Name2')
y = [7.39 7.12 6.44 6.04 5.63 5.40 5.10 4.96 4.80 4.50 4.33]; % Longueur d'onde
x = [0.0668 0.0755 0.0943 0.1022 0.1086 0.1254 0.1349 0.1460 0.1573 0.1660 0.1828]*1.5625; % Fréquence de rotation
plot(x,y,'k','DisplayName','Name3')
plot(x,y,'-o','DisplayName','Name4')
y = [7.39 6.58 5.92 5.74 5.27 5.04 4.83 4.73 4.28 4.23]; % Longueur d'onde
x = [0.0605 0.0777 0.0856 0.0938 0.1082 0.1193 0.1300 0.1466 0.1506 0.1680]*1.5625; % Fréquence de rotation
plot(x,y,'r','DisplayName','Name5')
plot(x,y,'-o','DisplayName','Name6');
hold off
grid on
ylabel('\lambda (cm)')
xlabel('\Omega (Hz)')
title('Mesure ruban rose largeur 1.5 cm avec une pince de 2.5 cm de largeur')
legend('NumColumns',3);
Since each pair of plots contains the same data, you lines exactly overlap.
  10 Comments
Wissem-Eddine Khatla
Wissem-Eddine Khatla on 12 Apr 2021
I am very sorry : I thaught that I have sent my message which isn't the case...
I have tried this one but I don't understand why it doesn't work :
%% TEST pince de 2.5 cm
y = [7.58 7.12 6.55 6.20 5.60 5.45 5.18 5.02 4.96 4.75 4.55 4.41]; % Longueur d'onde
x = [0.0755 0.0943 0.1022 0.1030 0.1187 0.1297 0.1430 0.1506 0.1634 0.1808 0.1898 0.1930]*1.5625; % Fréquence de rotation
z = [9 10 11 12 13 14 15 16 17 18 19 20]; % Nombre de volts
h = zeros(3,1);
plot(x,y,'b','DisplayName','7 cm')
h(1) = plot(x,y,'-o')
ylabel('\lambda (cm)')
xlabel('\Omega (Hz)')
hold on
y = [7.39 7.12 6.44 6.04 5.63 5.40 5.10 4.96 4.80 4.50 4.33]; % Longueur d'onde
x = [0.0668 0.0755 0.0943 0.1022 0.1086 0.1254 0.1349 0.1460 0.1573 0.1660 0.1828]*1.5625; % Fréquence de rotation
z = [8 9 10 11 12 13 14 15 16 17 18]; % Nombre de volts
plot(x,y,'k','DisplayName','10.5 cm')
h(2) = plot(x,y,'-o')
ylabel('\lambda (cm)')
xlabel('\Omega (Hz)')
y = [7.39 6.58 5.92 5.74 5.27 5.04 4.83 4.73 4.28 4.23]; % Longueur d'onde
x = [0.0605 0.0777 0.0856 0.0938 0.1082 0.1193 0.1300 0.1466 0.1506 0.1680]*1.5625; % Fréquence de rotation
z = [8 9 10 11 12 13 14 15 16 17]; % Nombre de volts
plot(x,y,'r','DisplayName','12.5 cm')
h(3) = plot(x,y,'-o')
ylabel('\lambda (cm)')
xlabel('\Omega R^{2}')
title('Mesure ruban rose largeur 1.5 cm avec une pince de 2.5 cm de largeur')
legend
legend(h,'pince de 2.5 cm','pince de 2.5 cm','pince de 2.5 cm');
grid on
hold off
%%
But you have already done it so thank you very much for your help. But could you tell me why my code doesn't do the job please ?
Thank you again.
Cris LaPierre
Cris LaPierre on 12 Apr 2021
Please explain what you mean by "doesn't work". Be specific about what you expected, and what it is doing instead.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!