Important plotting question (defining the markers in a for loop)
3 次查看(过去 30 天)
显示 更早的评论
Hi all,
If I want to run this code in a for loop and plot everything with different markers, how can I do that?
x = -2*pi:0.1:2*pi;
y1 = sin(x); y2 = sin(2*x); y3 = sin(3*x); y4 = sin(4*x); y5 = sin(5*x); y6 = sin(6*x);
plot(x,y1,'ro-'); hold on;
plot(x,y2,'bo-'); hold on;
plot(x,y3,'ko-'); hold on;
plot(x,y4,'r.-'); hold on;
plot(x,y5,'b.-'); hold on;
plot(x,y6,'k.-'); hold on;
0 个评论
采纳的回答
Image Analyst
2023-1-30
Try it this way:
x = -2*pi:0.1:2*pi;
colors = {'r', 'b', 'k', 'r', 'b', 'k'};
markers = {'o', 'o', 'o', '.', '.', '.'};
for k = 1 : 6
y = sin(k * x);
plot(x, y, '-', 'Color', colors{k}, 'Marker', markers{k});
hold on;
end
grid on;
legend
xlabel('x', 'FontSize',15)
ylabel('y', 'FontSize',15)
3 个评论
Image Analyst
2023-1-30
You can make the legend strings up like this
x = -2*pi:0.1:2*pi;
colors = {'r', 'b', 'k', 'r', 'b', 'k'};
markers = {'o', 'o', 'o', '.', '.', '.'};
for k = 1 : 6
y = sin(k * x);
plot(x, y, '-', 'Color', colors{k}, 'Marker', markers{k});
hold on;
legendStrings{k} = sprintf('Curve for y%d', k);
end
grid on;
legend(legendStrings, 'Location', 'Northwest');
xlabel('x', 'FontSize',15)
ylabel('y', 'FontSize',15)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!