when Plotting multiple lines using plot, how do adjust line color with jet?

6 ビュー (過去 30 日間)
Peter Brehm
Peter Brehm 2021 年 7 月 12 日
コメント済み: Peter Brehm 2021 年 7 月 13 日
You can pass a 2d Vector to plot and create multiple lines. I want to specify the color in vector format as well but cant figure out how to do it without a for loop. As an example I set up x and y as 2d matricies which describe concentric circles of radius 1 through 5. You can pass x and y directly to plot and it will create the desired plot with the default colors. I just want to adjust the colors using the jet function, but it seems it can only be done with a for loop. Is this possible?
R = 1:5; %Radius [1x5]
th = [0:360].'; %Theta [361x1]
x = cosd(th)*R; % [361x5]
y = sind(th)*R; % [361x5]
cmap = jet(length(R)); % [5x3]
figure
plot(x,y)
% plot(x,y,'Color',cmap.') %? This doesnt work!!
figure
hold on
for kk = 1:length(R)
plot(x(:,kk),y(:,kk),'Color',cmap(kk,:).')
end

採用された回答

Walter Roberson
Walter Roberson 2021 年 7 月 12 日
In a release after yours, R2019b, it became possible to use the new colororder() function in order to change the color of lines, including existing lines.
In your release (R2017b -- and thank you for including that information!), what you should do is set the axes ColorOrder property before drawing the lines:
R = 1:5; %Radius [1x5]
th = [0:360].'; %Theta [361x1]
x = cosd(th)*R; % [361x5]
y = sind(th)*R; % [361x5]
cmap = jet(length(R)); % [5x3]
fig = figure();
ax = axes(fig);
ax.ColorOrder = cmap;
hold(ax, 'on')
plot(ax, x,y)
hold(ax, 'off')
Be sure to have "hold" turned on for the axes, as ColorOrder is one of the properties that is replaced when MATLAB does its default plot initialization.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by