Marker edges, line width and legend

164 visualizzazioni (ultimi 30 giorni)
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 Commenti
Rik
Rik il 17 Nov 2021
You might need to create your own box that looks like a legend with a separate axes object.
Thomas Bissinger
Thomas Bissinger il 17 Nov 2021
Thank you for that. I was hoping there was a simpler way...

Accedi per commentare.

Risposta accettata

Rik
Rik il 17 Nov 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 Commento
Thomas Bissinger
Thomas Bissinger il 17 Nov 2021
Thanks. I guess this is the closest one can get without customizing the whole legend box...

Accedi per commentare.

Più risposte (1)

Chunru
Chunru il 17 Nov 2021
Modificato: Chunru il 17 Nov 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 Commento
Thomas Bissinger
Thomas Bissinger il 17 Nov 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.

Accedi per commentare.

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by