Marker edges, line width and legend
조회 수: 224 (최근 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
2021년 11월 17일
You might need to create your own box that looks like a legend with a separate axes object.
채택된 답변
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개)
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;
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!