Effacer les filtres
Effacer les filtres

Important plotting question (defining the markers in a for loop)

3 vues (au cours des 30 derniers jours)
Ashfaq Ahmed
Ashfaq Ahmed le 30 Jan 2023
Commenté : Ashfaq Ahmed le 1 Fév 2023
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;

Réponse acceptée

Image Analyst
Image Analyst le 30 Jan 2023
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 commentaires
Image Analyst
Image Analyst le 30 Jan 2023
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)
Ashfaq Ahmed
Ashfaq Ahmed le 1 Fév 2023
Excellent!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by