Plot outside a for loop with different plotstyles

1 vue (au cours des 30 derniers jours)
Christina
Christina le 9 Juin 2015
Commenté : Ingrid le 9 Juin 2015
Hello all,
I've got a vector a with 20 values and a for loop, through which I want to plot the variables var1 and b
%%%%%%
%Define the variables
a = 1:20;
b = 0:0.01:1;
n= numel(a);
m=numel(b);
var1 = zeros(n,m);
***********************
%% For loop
for i = 1:n
var1(i,:)=(2+b).^(a(i));
end
****************************
At the end, var is a matrix sized: 20 x 101 and b is a vector 1 x 101.
When I am plotting
figure
plot(b,var1)
%%%%%%%%%%%%%%%%%%%%
I've got a figure with 20 lines, however, these are specified by default. I need to define the colours and the plot styles for every single of those lines
For example, I want the 1st line to be --> b-,
the 2nd--> r-
the 3rd--> m-
the ..... 15th --> b.
and so on...
Any thoughts on that ?
Many thanks!

Réponses (1)

Ingrid
Ingrid le 9 Juin 2015
doc plot
will learn you this:
h = plot(_) returns a column vector of lineseries handles, where h contains one handle per line plotted. When multiple lines are present, you can make changes to properties of a specific line by specifying its handle.
so use this handle in combination with set There is even a complete example worked out showing to do what you want (Change Line Properties Using Handles) but you can also automize this in a for loop when you store your colors and linestyles in a matrix
  2 commentaires
Christina
Christina le 9 Juin 2015
Hi Ingrid,
When I am plotting
figure
plotStyle = {'b-','r-','g-','m-','c-','k-','y-', 'b.','r.','g.','m.','c.','k.','y.','b--','r--','g--','m--','c--','k--'};
plot(b,var1,plotStyle{i})
either in the for loop or outside the for loop, I just get a graph, where all of the 20 lines have the properties of the last style, in that case k--
How can I resolve this?
Ingrid
Ingrid le 9 Juin 2015
try
plot(b,var1(:,i),plotStyle{i})
inside the for loop
you need to index your var1 variable because now you are just plotting all 20 lines with the same linestyles 20 times, hence only showing the last style as this one is on top.
If you would open the editor you would see this behaviour

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line 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