Create a specific legend in Matlab

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--

Respuestas (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 comentarios

Wissem-Eddine Khatla
Wissem-Eddine Khatla el 11 de Abr. de 2021
Thank you for your answer,
I am sorry if my message wasn't clear : in fact, in the legend I need : three using lines with different colors and an additionnal one for the marker : in your example I see that the legend of the marker comes with the associated color of the line but what I need is just an additional line to specify the marker type in the legend (which is the circle marker here) I don't know if it is possible please ?
I hope that I have been clearier this time,
Thank you again.
Cris LaPierre
Cris LaPierre el 11 de Abr. de 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
Wissem-Eddine Khatla el 11 de Abr. de 2021
The plot that you have done is totally correct but the legend is not exactly what I want : in the graphic, the markers should be the same color as the line (like you did) but I wander if we can put for example in the legend : "Name1", "Name3", "Name5" and an additionnal legend that notifies only the circle marker which should be independant from the color of the line in the legend.
I don't know if I have been clear in my explanations because it seems a little bit tricky,
Thank you.
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
Wissem-Eddine Khatla el 11 de Abr. de 2021
I think it’s very close to what I was expected !
But now, for example, is it possible to plot the other two lines with different colors but with this same red circle markers so we can notify a single line in the legend for the marker and three other lines for the colors ?
Which make a total of four lines for the legend.
Thank you.
Cris LaPierre
Cris LaPierre el 11 de Abr. de 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
Wissem-Eddine Khatla el 11 de Abr. de 2021
You are right : I will try that and come back if I have a problem.
Thank you for your help and I wish that you would help me again if I don’t find the solution.
Thank you again,
Cris LaPierre
Cris LaPierre el 12 de Abr. de 2021
Editada: Cris LaPierre el 12 de Abr. de 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
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 el 12 de Abr. de 2021
Please explain what you mean by "doesn't work". Be specific about what you expected, and what it is doing instead.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 11 de Abr. de 2021

Comentada:

el 12 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by