Plot lines with different colors each iteration

9 ビュー (過去 30 日間)
Geriffa
Geriffa 2018 年 5 月 12 日
編集済み: Image Analyst 2018 年 5 月 12 日
Hi,
The following code plots 5 lines, all in black. I want them different in colors for each iteration. This section is within a large loop.
Q = volymflode*3600;
H = DeltaPF_TOT/(((rho_varm+rho_kall)/2)*g);
vektor_Q(1,j)=Q;
vektor_H(1,j)=H;
figure(1);
title('Systemkurva');
plot(vektor_Q,vektor_H,'k-','LineWidth',1);
ylabel('Uppfodringshöjd [m]');
xlabel('Volymflöde [M^3/h]');
plot(Qpump1,Hpump1,'b-')
j=j+1;
hold on
How do i make this happen?

回答 (2 件)

Mohammad Alhashash
Mohammad Alhashash 2018 年 5 月 12 日
I've I assumed the loop index to be ii, then you can modify the plot portion of your code to be:
colorInd='ckyrb';
plot(vektor_Q,vektor_H,['-' colorInd(ii)],'LineWidth',1);
In this case, the color will be in the following order: 1.cyan 2.black 3.yallow 4.red 5.blue You can use whatever color you want by typing different symbols in colorInd variable.This may help also : https://www.mathworks.com/help/matlab/ref/linespec.html
  1 件のコメント
Geriffa
Geriffa 2018 年 5 月 12 日
編集済み: Geriffa 2018 年 5 月 12 日
Should've mentioned it's within a while loop so no direct loop index exists, so I don't think this will work in my case. Got any another ideas more easily implemented in a while loop?
Edit: The while loop uses a counter and the loop stops when the counter reaches 50.

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


Image Analyst
Image Analyst 2018 年 5 月 12 日
編集済み: Image Analyst 2018 年 5 月 12 日
Simply get rid of the k
plot(vektor_Q, vektor_H, '-', 'LineWidth', 1);
ylabel('Uppfodringshöjd [m]');
xlabel('Volymflöde [M^3/h]');
hold on;
plot(Qpump1, Hpump1, 'b-', 'LineWidth', 2)
That will use the defined color order for plots, one of which is probably blue, so that's why I set the linewidth of your blue plot to 2 so you can tell the difference. If you want to specify the colororder, see attached demos.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by