How do I add a key to my plot?

My plot has four variables in it (x1,y1,x2,y2) representing two EN coordinates. How do I add a key to show which is which?
Also, it would be useful if I could ensure the x and y had the same scale (but not a fixed scale).

1 comentario

DGM
DGM el 23 de Feb. de 2022
You mean legend()?
For the second part, can you use axis equal or axis square? If not, you'll need to clarify what you want.

Iniciar sesión para comentar.

 Respuesta aceptada

Voss
Voss el 23 de Feb. de 2022
Editada: Voss el 23 de Feb. de 2022
To add a key showing which line is which, you can use legend().
To ensure the same x- and y-scale, you can use axis equal.
figure();
% random data with different y-scales
line_x1 = plot(randn(1,10),4*randn(1,10));
hold on
line_y1 = plot(randn(1,10),3*randn(1,10));
line_x2 = plot(randn(1,10),2*randn(1,10));
line_y2 = plot(randn(1,10),randn(1,10));
% equal x- and y-scale
axis equal
% make a line key (a.k.a. legend)
names = {'x1','y1','x2','y2'};
legend([line_x1 line_y1 line_x2 line_y2],names);

1 comentario

Great answer. I often need to use underscores in my plot titles and legends. So had that been the case here, you can add ,'Interpreter', 'none' to the legend:
figure();
% random data with different y-scales
line_x1 = plot(randn(1,10),4*randn(1,10));
hold on
line_y1 = plot(randn(1,10),3*randn(1,10));
line_x2 = plot(randn(1,10),2*randn(1,10));
line_y2 = plot(randn(1,10),randn(1,10));
% equal x- and y-scale
axis equal
% make a line key (a.k.a. legend)
names = {'line_x1','line_y1','line_x2','line_y2'};
legend([line_x1 line_y1 line_x2 line_y2],names,'Interpreter', 'none');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Polar Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 23 de Feb. de 2022

Comentada:

el 25 de Jun. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by