Is it possible to have a line graph with a different marker for each point on the same line?
21 views (last 30 days)
Show older comments
x = 1:5;
y1 = [2, 3, 1, 4, 2];
y2 = [1, 4, 2, 3, 5];
figure;
plot(x, y1, '-o', 'DisplayName', 'Line 1', 'MarkerSize', 8, 'LineWidth', 1.5);
hold on;
plot(x, y2, '-s', 'DisplayName', 'Line 2', 'MarkerSize', 8, 'LineWidth', 1.5);
hold off;
xlabel('X-axis');
ylabel('Y-axis');
title('Line Graph with Different Markers');
legend('Location', 'Best'); % Adjust the legend position
grid on;
0 Comments
Accepted Answer
Matt J
on 31 Jan 2024
Edited: Matt J
on 31 Jan 2024
You can effect that as below. I don't see why you'd want it, however.
x = 1:5;
y = [2, 3, 1, 4, 2];
xc=num2cell(x); xc{end+1}=x;
yc=num2cell(y); yc{end+1}=y;
mc={'x','s','v','^','+','-b'}; %Marker and line styles
args=[xc;yc;mc];
H=plot(args{:}); axis padded
h=H(1:end-1);
set(h,'Color','r');
[h.MarkerFaceColor]=deal(h.Color);
1 Comment
Walter Roberson
on 31 Jan 2024
The key here is that any one plot line can only have a single marker, so first you draw the continuous line and then you add in markers point by point.
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!