Put a single legend on a 2D vector plot
27 views (last 30 days)
Show older comments
Camille
on 12 Dec 2024 at 21:36
Commented: Adam Danz
on 13 Dec 2024 at 20:06
Hi guys,
I have two pairs of 2D matrices. Each matrice contains 43 lines and 1001 columns. The first line of the first vector corresponds to the x coordinates of a ray. The first line of the second vector corresponds to the y coordinates of the same ray. The remaining lines follows the same logic. I managed to plot the 43 rays of my two pairs of vectors. (incident rays and reflected rays)
The problem is that the legend i added makes me a 43*2 lines legend. it adds a legend for every ray. If you take a look at the picture below. How can i have a legend with one line for the 43 red rays and one line for the 43 blue rays please ?
I attached my full code. The part im asking about goes from line 99 to 112 in the file 'trace_rayon.m'. (
Thank you
figure;
plot(rayon_vect_x, rayon_vect_y,'r--', 'DisplayName','Incident ray');
hold on
plot(rayon_ref_X.', rayon_ref_Y.','b--', 'DisplayName','Reflected ray');
hold off;
legend('show');
xlabel('X');
ylabel('Y');
axis equal;
grid("on");
0 Comments
Accepted Answer
Walter Roberson
on 12 Dec 2024 at 21:47
figure;
L1 = plot(rayon_vect_x, rayon_vect_y,'r--', 'DisplayName','Incident ray');
hold on
L2 = plot(rayon_ref_X.', rayon_ref_Y.','b--', 'DisplayName','Reflected ray');
hold off;
legend([L1(1), L2(1), 'show');
xlabel('X');
ylabel('Y');
axis equal;
grid("on");
2 Comments
Adam Danz
on 13 Dec 2024 at 20:06
legend([L1(1), L2(1)], 'show');
The line above applies the string "show" as the DisplayName for the first object L1(1). It does not toggle the visibility of the legend to on as is the case with legend('show').
When using the flag syntaxes with legend, be sure not to combine the flag arguments with other inputs. They are meant to be the sole input argument. In some cases, it may seem like the combination works but it often causes issues.
More Answers (1)
Voss
on 12 Dec 2024 at 22:07
[Note: I think you meant to transpose rayon_vect_x and rayon_vect_y in the first plot() call (so as to create 43 lines instead of 1001), so I'm making that assumption throughout this answer. That assumption does not affect the methods proposed below.]
One way to set up the legend properly is to tell legend() only to make entries for the first line of each type (incident/reflected):
figure;
h_incident = plot(rayon_vect_x.', rayon_vect_y.','r--', 'DisplayName','Incident ray');
hold on
h_reflected = plot(rayon_ref_X.', rayon_ref_Y.','b--', 'DisplayName','Reflected ray');
hold off;
legend([h_incident(1) h_reflected(1)]);
xlabel('X');
ylabel('Y');
axis equal;
grid("on");
Another way is to create only one line of each type containing all the data for that type, using NaNs to create breaks in the line:
figure;
x_plot = rayon_vect_x.';
y_plot = rayon_vect_y.';
x_plot(end+1,:) = NaN;
y_plot(end+1,:) = NaN;
plot(x_plot(:), y_plot(:),'r--', 'DisplayName','Incident ray');
hold on
x_plot = rayon_ref_X.';
y_plot = rayon_ref_Y.';
x_plot(end+1,:) = NaN;
y_plot(end+1,:) = NaN;
plot(x_plot(:), y_plot(:),'b--', 'DisplayName','Reflected ray');
hold off
legend('show');
xlabel('X');
ylabel('Y');
axis equal;
grid("on");
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!