Marker edges, line width and legend
224 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Thomas Bissinger
am 17 Nov. 2021
Kommentiert: Thomas Bissinger
am 17 Nov. 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 Kommentare
Rik
am 17 Nov. 2021
You might need to create your own box that looks like a legend with a separate axes object.
Akzeptierte Antwort
Rik
am 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);
Weitere Antworten (1)
Chunru
am 17 Nov. 2021
Bearbeitet: Chunru
am 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;
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!