How do I set the plot color and line style of several data sets using a cell array?

5 views (last 30 days)
I am plotting several sets of data on a single plot and would like to control the color and linestyle of each line using a cell array like this:
figure(1)
hold on
grid on
xlim([0 60])
colorVec = {'b', 'r', 'y', 'p', 'g', 'c'};
linVec = {'--', '-.','--', '-.','--', '-.'};
yyaxis left
plot(tvar,Ts1(:,:),'Color',colorVec{:},'LineStyle',linVec{:},'LineWidth',2,'MarkerSize',5)
hold off
However, the above code does not work and produces an error. Can anyone help me with the syntax of what I am trying to accomplish? Thank You in advance.

Answers (1)

KSSV
KSSV on 23 Apr 2018
figure(1)
hold on
grid on
xlim([0 60])
colorVec = {'b', 'r', 'y', 'm', 'g', 'c'};
linVec = {'--', '-.','--', '-.','--', '-.'};
yyaxis left
for i = 1:6
plot(tvar,Ts1(:,i),'Color',colorVec{i},'LineStyle',linVec{i},'LineWidth',2,'MarkerSize',5)
end
hold off
  3 Comments
Lana Piner
Lana Piner on 17 May 2019
Not sure, if this helps and it has been a while, but if you set default colors ahead of time, you do not need to call them in the loop - they will be picked automagically in the order they are listed.
set(groot,'defaultAxesColorOrder',[1 0 0;0 1 0;0 0 1])
This is done any time before you hold figure and do the loop.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks 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!