Plot outside a for loop with different plotstyles

1 次查看(过去 30 天)
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!

回答(1 个)

Ingrid
Ingrid 2015-6-9
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 个评论
Christina
Christina 2015-6-9
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 2015-6-9
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

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by