how to do plotting with different colors in same figure?
504 次查看(过去 30 天)
显示 更早的评论
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
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
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
2013-11-6
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 个评论
Kamilu Sanusi
2023-5-1
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
更多回答(7 个)
Kelly Kearney
2013-11-6
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 个评论
Image Analyst
2013-11-6
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.
Korosh Agha Mohammad Ghasemi
2020-12-7
编辑:Korosh Agha Mohammad Ghasemi
2020-12-7
%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
0 个评论
PeopleMATLAB
2016-10-13
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 个评论
farhat khan
2019-7-19
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
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
2020-12-7
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
0 个评论
Da Bu
2021-4-22
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!