how to include symbol on plot line into the legend?

35 次查看(过去 30 天)
To make my plots clear in greyscale, I'm using symbols and I want these to appear on the legend as well as the line colour. Does anyone know how to do this?
plot(IPA5a(end-1000:end,1)*10^-6, IPA5a(end-1000:end,3)*10^9,'Color','g','LineWidth',1)
hold on
plot(IPA5a(end-1000:20:end,1)*10^-6, IPA5a(end-1000:20:end,3)*10^9,'<','Color','g')
legend({'IPA'},'Location','southeast')

回答(2 个)

C B
C B 2023-8-16
Is this what you are looking for using this symbol '<' ?
n = 2000;
IPA5a = [linspace(0, 2*pi, n)' sin(linspace(0, 2*pi, n))'];
% Plotting the continuous line
p1 = plot(IPA5a(end-1000:end,1)*10^-6, IPA5a(end-1000:end,2)*10^9, ...
'Color','k', 'LineWidth',1, 'LineStyle','-'); % 'k' for black
hold on
% Overlaying the markers
p2 = plot(IPA5a(end-1000:20:end,1)*10^-6, IPA5a(end-1000:20:end,2)*10^9, ...
'<', 'Color','k', 'MarkerFaceColor','k');
% Adding the legend to reflect both line and marker
legend([p1, p2], {'IPA Line', 'IPA Marker'}, 'Location', 'southeast');
hold off

Star Strider
Star Strider 2023-8-16
The code plots two lines, however has only one legend entry, so the second series (with the '<' marker) does not show up in the legend. Use '<g' to plot the markers, or '<-g' to plot the markers with a line linking them. That aside, both lines are the same colours, so that may make them a bit difficult to distinguish.
IPA5a = rand(2000,3)+[0 1 2]+1E10;
IPA5a(:,1) = sort(IPA5a(:,1));
figure
plot(IPA5a(end-1000:end,1)*10^-6, IPA5a(end-1000:end,3)*10^9,'Color','g','LineWidth',1)
hold on
plot(IPA5a(end-1000:20:end,1)*10^-6, IPA5a(end-1000:20:end,3)*10^9,'<g')
legend({'IPA','IPA<'},'Location','southeast')
.

类别

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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by