Marker edges, line width and legend

232 次查看(过去 30 天)
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 个评论
Rik
Rik 2021-11-17
You might need to create your own box that looks like a legend with a separate axes object.
Thomas Bissinger
Thomas Bissinger 2021-11-17
Thank you for that. I was hoping there was a simpler way...

请先登录,再进行评论。

采纳的回答

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

请先登录,再进行评论。

更多回答(1 个)

Chunru
Chunru 2021-11-17
编辑:Chunru 2021-11-17
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 个评论
Thomas Bissinger
Thomas Bissinger 2021-11-17
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by