Marker edges, line width and legend

240 visualizaciones (últimos 30 días)
Thomas Bissinger
Thomas Bissinger el 17 de Nov. de 2021
Comentada: Thomas Bissinger el 17 de Nov. de 2021
EDIT: Sorry, the original question's MWE was too simple and did not include the problem with the dashed lines not overlaying.
Hi there,
I am trying to create a figure featuring markers with black edges on top of a dashed line. Apparently, it is not possible to set the marker edge line width independently from the dashed line's line width, as they are both set by the option 'LineWidth'. The recommended workaround I found was to plot the line and the markers separately, which works for the image itself, but then I couldn't find a way to get the legend right. Finally, I can overlay two lines with different line width, leading to a legend that looks almost right (except for a not-too-noticeable difference in line width), but here the dashed property of the line comes in the way because two dashed lines of different width, one of which features markers, don't overlay well.
Here's my MWE:
xx=linspace(0,12);
figure(1)
plot(xx,sin(xx),'LineStyle','--','LineWidth',2,...
'Color','red', 'HandleVisibility','off');
hold on;
plot(xx,sin(xx),'LineStyle','none','LineWidth',1,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'MarkerIndices',1:5:length(xx),...
'Displayname','sin(x)');
legend show;
figure(2)
plot(xx,sin(xx),'LineStyle','--','LineWidth',2,...
'Color','red', 'HandleVisibility','off');
hold on;
plot(xx,sin(xx),'LineStyle','--','LineWidth',1,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'MarkerIndices',1:5:length(xx),...
'Displayname','sin(x)');
legend show;
figure(3)
plot(xx,sin(xx),'LineStyle','--','LineWidth',2,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'MarkerIndices',1:5:length(xx),...
'Displayname','sin(x)');
legend show;
Figure 1 is what I want plot-wise: thick dashed line, thin edges. But the legend isn't what I am looking for. Figure 2 combines two lines, the legend looks ok, but the different line widths lead to a weird overlay of dashed lines. Figure 3 finally has sort of the legend I want, but the markers' edges are too thick.
Thanks for any help
  2 comentarios
Rik
Rik el 17 de Nov. de 2021
You might need to create your own box that looks like a legend with a separate axes object.
Thomas Bissinger
Thomas Bissinger el 17 de Nov. de 2021
Thank you for that. I was hoping there was a simpler way...

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 17 de Nov. de 2021
Or you use a line object without any points to get the legend entry you want:
xx=linspace(0,12);
plot(xx,sin(xx),'LineStyle','--','LineWidth',2,...
'Color','red', 'HandleVisibility','off');
hold on;
plot(xx,sin(xx),'LineStyle','none','LineWidth',1,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'MarkerIndices',1:5:length(xx));
h_plot_style_legend=...
plot(NaN*xx,NaN*sin(xx),'LineStyle','--','LineWidth',2,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'Displayname','sin(x)',...
'MarkerIndices',1:5:length(xx));
legend(h_plot_style_legend);
  1 comentario
Thomas Bissinger
Thomas Bissinger el 17 de Nov. de 2021
Thanks. I guess this is the closest one can get without customizing the whole legend box...

Iniciar sesión para comentar.

Más respuestas (1)

Chunru
Chunru el 17 de Nov. de 2021
Editada: Chunru el 17 de Nov. de 2021
xx=linspace(0,12);
figure(1)
plot(xx,sin(xx),'LineStyle','-','LineWidth',2,...
'Color','red', 'HandleVisibility','off');
hold on;
% The LineStyle of the next line should be "-"if you want te legend to have
% a line.
plot(xx,sin(xx),'LineStyle','-', 'LineWidth',1,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'Displayname','sin(x)');
legend show;
figure(2)
plot(xx,sin(xx),'LineStyle','-','LineWidth',2,...
'Marker','o','MarkerSize',6,...
'Color','red','MarkerEdgeColor','black','MarkerFaceColor','red',...
'Displayname','sin(x)');
legend show;
  1 comentario
Thomas Bissinger
Thomas Bissinger el 17 de Nov. de 2021
Thank you for the answer. I am sorry, my original MWE was too minimal, I forgot to include the problems that come up when working with dashed lines. Apart from that, your answer is of course correct if we accept that the legend's line is a bit thinner than the plot's.

Iniciar sesión para comentar.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by