Create a specific legend in Matlab
Afficher commentaires plus anciens
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--
Réponses (1)
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 commentaires
Wissem-Eddine Khatla
le 11 Avr 2021
Cris LaPierre
le 11 Avr 2021
Legends take their color, style, and marker properties from the associated plot. You therefore have to plot something with the appearance you want to add it to your legend. That leaves you with a choice. What color should the markers be?
Wissem-Eddine Khatla
le 11 Avr 2021
So you want all 6 labels?
What icon is displayed in the legend is directly linked to how you plotted that object. If you don't want a line, then don't include a line style in you linespec.
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')
% ^^^ I removed the line style, which was '-'
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
Wissem-Eddine Khatla
le 11 Avr 2021
Cris LaPierre
le 11 Avr 2021
Yes but now I'm going to make you do a little work. Look at the linespec link in my last post for how to do that. You may have to use plot objects or change the order you plot in to get what you want. Give it a shot and share your code if you get stuck.
Wissem-Eddine Khatla
le 11 Avr 2021
Cris LaPierre
le 12 Avr 2021
Modifié(e) : Cris LaPierre
le 12 Avr 2021
I didn't mean to leave you hanging. It appears to be taking you longer than I intended.
Here's one way that uses the line objects and linespec to create the desired legend.
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
h(1) = plot(x,y,'b','DisplayName','Name1');
hold on
h(2) = plot(x,y,'ro','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
h(3) = plot(x,y,'k','DisplayName','Name3');
h(4) = plot(x,y,'ro','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
h(5) = plot(x,y,'r','DisplayName','Name5');
h(6) = plot(x,y,'ro','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(h([1 3 5 2]));
Here's another way to accomplish the same thing by plotting all the data points in one plot command. Each data set is given a unique variable name. I don't specify a color for the markers, so it uses the next one in the colororder.
y1 = [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
x1 = [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
figure
plot(x1,y1,'b','DisplayName','Name1')
hold on
y2 = [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
x2 = [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(x2,y2,'k','DisplayName','Name2')
y3 = [7.39 6.58 5.92 5.74 5.27 5.04 4.83 4.73 4.28 4.23]; % Longueur d'onde
x3 = [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(x3,y3,'r','DisplayName','Name3')
plot([x1 x2 x3],[y1 y2 y3],'o','DisplayName','Name4');
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
Wissem-Eddine Khatla
le 12 Avr 2021
Cris LaPierre
le 12 Avr 2021
Please explain what you mean by "doesn't work". Be specific about what you expected, and what it is doing instead.
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



