how to do plotting with different colors in same figure?

My sample code is this:
x=[1:100];
for i=1:5
y(:,i)=i*log(x);
end
This plots 5 different curves of same color.
How to plot each curve with a different color ?

2 件のコメント

CPS
CPS 2017 年 3 月 2 日
x=[1:100];
for i=1:5
y(:,i)=i*log(x);
end
plot(x,y)
This will generate an automatic graph.
Image Analyst
Image Analyst 2017 年 3 月 2 日
This is not a comment so you should put it with the rest of the "Answers" below (so you could get "reputation points" for it if someone "Votes" for it).

サインインしてコメントする。

 採用された回答

Simon
Simon 2013 年 11 月 6 日

7 投票

Hi!
If you plot like
plot(x,y)
and y is a matrix, you get plots with different color automatically. But you can do something like this to get manual coloring:
colorstring = 'kbgry';
figure(1); cla;
hold on
for i = 1:5
plot(x,y(:, i), 'Color', colorstring(i))
end

7 件のコメント

Ved
Ved 2013 年 11 月 6 日
編集済み: Ved 2013 年 11 月 6 日
@Simon: Thank You.
Could you please tell what if there are many plots.
Say,20 plots.(i.e., i=20) and also the legend ( say i=1,i=2, ... ,i=20) for 20 plots.
Simon
Simon 2013 年 11 月 6 日
Hi!
Define your colors as RGB values like
colorspec = {[0.9 0.9 0.9]; [0.8 0.8 0.8]; [0.6 0.6 0.6]; ...
[0.4 0.4 0.4]; [0.2 0.2 0.2]};
figure(1); cla;
hold on
for i = 1:5
plot(x,y(:, i), 'Color', colorspec{i})
end
Mind the '{}' now in the plot command! You may define as many colors as you like.
If you want to have repeating colors (for plots with many lines useful), try
x=[1:100];
for i=1:20
y(:,i)=i*log(x);
end
colorspec = {[0.9 0.9 0.9]; [0.8 0.8 0.8]; [0.6 0.6 0.6]; ...
[0.4 0.4 0.4]; [0.2 0.2 0.2]};
linespec = {'-', ':', '-.', '--'};
figure(1); cla;
hold on
for i = 1:20
plot(x,y(:, i), 'Color', colorspec{mod(i,5)+1}, 'LineStyle', ...
linespec{floor(i/5)+1})
end
Ved
Ved 2013 年 11 月 10 日
Thanks a Lot, Simon.
But i am getting all 20 curves in different gray shades.
Simon
Simon 2013 年 11 月 11 日
Hi!
Of course you have to define a usable colorspec. Take a look at http://en.wikipedia.org/wiki/RAL_colour_standard and follow the links there. There are many colors with their RGB values. Mind that matlab uses RGB values between 0 and 1, and not between 0 and 255!
Kamilu Sanusi
Kamilu Sanusi 2023 年 5 月 1 日
I would appreciate if you can help me with this problem. I have a system matrix A containing a damping variable D of which I want to see how it affects the position of eigenvalues on pzmap.
  1. Please how do I link the eigen values of the same value of D together by a line?
  2. How do i link only corresponding eigenvalue Lambda1 of different value oF D together, and the same thing applicable to other corresponding values of lampdas on the pz maz
  3. with arrow showing possible direction of motion of eigenvalue as a result of change in D
Ta1 = 24; Ta2 = 27; Ta3= 20;
H11 = -0.0641; H12 = 0.0359;
H21 = 0.1176; H22 = -0.2057;
H31 = 0.2077; H32 = 0.1961;
for D = [0 7 10 15 20 25 40 70]
A = [0 0 1 0 -1;0 0 0 1 -1;(-H11/Ta1) (-H12/Ta1) (-D/Ta1) 0 0;...
(-H21/Ta2) (-H22/Ta2) 0 (-D/Ta2) 0;(-H31/Ta3) (-H32/Ta3) 0 0 (-D/Ta3)];
Eig = eig(A);
a = Eig(1,1);
b = Eig(2,1);
c = Eig(3,1);
d = Eig(4,1);
e = Eig(5,1);
s = tf('s');
T = (1)/((s-a)*(s-b)*(s-c)*(s-d)*(s-e));
P = pole(T);
if D == 7
pzmap(T)
end
hold on
if D == 15
pzmap(T)
end
hold on
if D == 25
pzmap(T)
end
end
Kamilu Sanusi
Kamilu Sanusi 2023 年 5 月 1 日
@Image Analyst, @CPS, @Kelly Kearney how do i make the marker thick on the pzmap please?
Image Analyst
Image Analyst 2023 年 5 月 2 日
編集済み: Image Analyst 2023 年 5 月 2 日
@Kamilu Sanusi sorry I don't have that function on my computer. See if there is a 'LineWidth' or 'MarkerSize' option for it.
help pzmap
PZMAP Pole-zero map of dynamic systems. PZMAP(SYS) computes the poles and (transmission) zeros of the dynamic system SYS and plots them in the complex plane. The poles are plotted as x's and the zeros are plotted as o's. PZMAP(SYS1,SYS2,...) shows the poles and zeros of several systems SYS1,SYS2,... on a single plot. You can specify distinctive colors for each model, for example: pzmap(sys1,'r',sys2,'y',sys3,'g') [P,Z] = PZMAP(SYS) returns the poles and zeros of the system in two column vectors P and Z. No plot is drawn on the screen. The functions SGRID or ZGRID can be used to plot lines of constant damping ratio and natural frequency in the s or z plane. For arrays SYS of dynamic systems, PZMAP plots the poles and zeros of each model in the array on the same diagram. See PZPLOT for additional graphical options for pole/zero plots. See also PZPLOT, POLE, ZERO, SGRID, ZGRID, RLOCUS, DYNAMICSYSTEM. Documentation for pzmap doc pzmap Other uses of pzmap DynamicSystem/pzmap lti/pzmap

サインインしてコメントする。

その他の回答 (7 件)

Kelly Kearney
Kelly Kearney 2013 年 11 月 6 日

13 投票

Also, using colormaps and set with multiple handles + cell arrays can be helpful if you need a lot of colors.
x = 1:100;
ii = (1:20)';
y = ii*log(x);
h = plot(x,y);
set(h, {'color'}, num2cell(jet(length(ii)), 2));

3 件のコメント

Ved
Ved 2013 年 11 月 10 日
thanks a lot kelly !
Sergey Kasyanov
Sergey Kasyanov 2019 年 6 月 14 日
Thanks!
Saksham Gakhar
Saksham Gakhar 2021 年 4 月 19 日
thank you!

サインインしてコメントする。

Image Analyst
Image Analyst 2013 年 11 月 6 日

2 投票

Simon showed you how to specify custom colors for each curve. If you want to know how to change the default color order, see my demo attached below.

1 件のコメント

Ved
Ved 2013 年 11 月 10 日
thank you Image Analyst.

サインインしてコメントする。

Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi 2020 年 12 月 7 日
編集済み: Korosh Agha Mohammad Ghasemi 2020 年 12 月 7 日

1 投票

%https://zil.ink/korosh -------- Ways to contact me ----------
% Korosh Agha Mohammad Ghasemi !
% Chemical Engineering at Shiraz University
x=linspace(0,2,100);
figure;
for a=[0.1 0.5 1 2 4]
y=x.^a; %The function is hypothetical
if a == 0.1 %Any color can be substituted
y=x.^a;
plot(x,y,'k') %Now choose the color
hold on
elseif a == 0.5
y=x.^a;
plot(x,y,'b') %Now choose the color
hold on
elseif a==1
y=x.^a;
plot(x,y,'g') %Now choose the color
hold on
elseif a==2
y=x.^a;
plot(x,y,'r') %Now choose the color
hold on
elseif a==4
y=x.^a;
plot(x,y,'y') %Now choose the color
hold on
grid on
end
end
PeopleMATLAB
PeopleMATLAB 2016 年 10 月 13 日

0 投票

colormap jet;
cmap=colormap;
for i=1:5
Plot_color=cmap(i/5,:);
plot(x, y(i,:), 'Color', Plot_color);
hold on;
end
hold off;

2 件のコメント

K275
K275 2016 年 11 月 15 日
FYI: This code generates an error because i/5 is not an integer or logical.
C S
C S 2018 年 7 月 11 日
just use i*5 and you are okay

サインインしてコメントする。

farhat khan
farhat khan 2019 年 7 月 19 日

0 投票

gys i have a question ..... how to detect the object with color based detection if the required object have more then one color e.g green and black ..........

1 件のコメント

Image Analyst
Image Analyst 2019 年 7 月 19 日
I don't beleive you can plot a single set of data with two colors and one call to plot (plotyy notwithstanding). You have to keep track of the handle of the things you plotted:
hGreen = plot(x1, y1, 'g-'); % Plot a green line.
hBlack = plot(x2, y2, 'k-'); % Plot a black line.
I'm not sure how you'd detect what the color was if you didn't save the handle to the colored line you plotted, but maybe there is a way by using findobj() and seeing if you can get the color of any line object as a property. It's certainly not as straightforward so I recommend you just save the handles to the colors of the lines you drew, if you plan to detect them later.

サインインしてコメントする。

SYAHIRAH ZAKI
SYAHIRAH ZAKI 2020 年 12 月 7 日

0 投票

hi does anoyne know how to get different plot colors for each of my graph?
if i change the 'r' to 'g', i only get one color for each of my graph. I need each of my plot in different colour. please help me :(
% Script file graph2.
% Several plots of the rational function y = x/(1+x^2)
% in the same window.
k = 0;
for n=1:3:10
n10 = 10*n;
x = linspace(-2,2,n10);
y = x./(1+x.^2);
k = k+1;
subplot(2,2,k)
plot(x,y,'r')
title(sprintf('Graph %g. Plot based upon n = %g points.' ...
, k, n10))
xlabel('x')
ylabel('y')
axis([-2,2,-.8,.8])
grid
pause(3);
end
Da Bu
Da Bu 2021 年 4 月 22 日

0 投票

for a small set of colors, you can use ColorOrderIndex
t=1:0.1:2*pi;
for i=1:5
plot(t,sin(t)*i), hold on
ax = gca;
ax.ColorOrderIndex = i;
end

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

タグ

質問済み:

Ved
2013 年 11 月 6 日

編集済み:

2023 年 5 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by