Colormap for multiple plots

20 ビュー (過去 30 日間)
Sha
Sha 2020 年 9 月 28 日
コメント済み: Image Analyst 2020 年 9 月 29 日
Hi,
I have a matrix K of size N x n and I plot the values of K on the same figure by using "hold on" n plots with N values each.
I would like to use a color map (from -1 to 1 , Blue to Red) in the following way: the n-th plot has color "0.4" or yellow if the first value of the column K(1,n) = 0.4 and so on.
How can I achieve this please?
Here is the code I have been trying to play with ( in particular adding the option 'Color' then 'jetcustom' in the plot options returns an error, I was trying to do like in this answer MathWorks Answer)
colormap(jet(n));
jetcustom=jet(n);
figure
x=1:k;
for i=1:n
y=K(:,i);
I=ismember(i,A);
J=ismember(i,B);
if I==1
plot(x,y,'--');
elseif J==1
plot(x,y);
end
hold on
end
xlim([1 k]);
ylim([-1.5 1.5])
colormap(jet(10))
cb=colorbar;
caxis([-1 1])
  1 件のコメント
Sha
Sha 2020 年 9 月 28 日
I used both answers below! Thank you!
jetcustom=jet(n);
r1=K(1,:);
colors = interp1(linspace(-1, 1, n), jetcustom, r1.');
figure()
set(gca, 'ColorOrder', colors , 'NextPlot', 'replacechildren');
x=1:k;
for i=1:n
y=K(:,i);
I=ismember(i,Con);
J=ismember(i,Ex);
if I==1
plot(x,y,'--');
elseif J==1
plot(x,y);
end
hold on
end
xlim([1 k]);
ylim([-1.5 1.5])
colormap(jet(10))
cb=colorbar;
caxis([-1 1])

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

採用された回答

Image Analyst
Image Analyst 2020 年 9 月 28 日
See my attached demo.
  4 件のコメント
Sha
Sha 2020 年 9 月 28 日
Thanks, I can and I will, I just wanted to accept both answers as I used both to solve my problem but MathWorks doesn't allow me to do so;
Image Analyst
Image Analyst 2020 年 9 月 29 日
Correct. However, if you like other answers, you can also award them the same number of "reputation points" (as an Accept) by clicking the "Vote" thumbs up icon. Thanks again. Feel free to come back anytime.

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 9 月 28 日
編集済み: Ameer Hamza 2020 年 9 月 28 日
colormap is not used for deciding the color of plot() lines. For that, you need to modify ColorOrder property. Something like this should work.
jetcustom = jet(n);
r1 = K(1, :); % first row decide the colors
colors = interp1(linspace(-1, 1, n), jetcustom, r1.');
figure()
ax = axes()
ax.ColorOrder = colors; % colororder(ax, colors)
x=1:k;
for i=1:n
y=K(:,i);
I=ismember(i,A);
J=ismember(i,B);
if I==1
plot(x,y,'--');
elseif J==1
plot(x,y);
end
hold on
end
xlim([1 k]);
ylim([-1.5 1.5])
colormap(jet(10))
cb=colorbar;
caxis([-1 1])
  2 件のコメント
Sha
Sha 2020 年 9 月 28 日
Thank you very much seems like a great idea to fix the first line for the colors, but sadly I still get the same figure. Any idea why the plot is not taking into account the ColorOrder?
Sha
Sha 2020 年 9 月 28 日
Thank you it worked with this slight modification:
jetcustom=jet(n);
r1=K(1,:);
colors = interp1(linspace(-1, 1, n), jetcustom, r1.');
figure()
set(gca, 'ColorOrder', colors , 'NextPlot', 'replacechildren');

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by