how to give the lines color based on the distanse

4 ビュー (過去 30 日間)
H-M
H-M 2020 年 7 月 16 日
コメント済み: H-M 2020 年 7 月 18 日
My plot below shows the oscillatory displacement of particles vs time in a pipe. Each line coreponds to each particle. Particles are released in different position from the center of pipe along the radius. How can I show each line in this plot in a different color?(My goal is the line's color changes with the distance from the center of pipe(r)).
r=sqrt(Xp^2+Yp^2): position of each particle in rdaial direction.Xp and Yp are the particle position in cartesian.

採用された回答

Image Analyst
Image Analyst 2020 年 7 月 16 日
Try this (untested)
cmap = jet(length(Xp));
r = sqrt(Xp .^ 2 + Yp .^ 2);
r = round(rescale(r, 1, length(Xp)));
for k = 1 : length(Xp)
thisColor = cmap(r(k), :);
plot(Xp(k), Yp(k), '.', 'Color', thisColor, 'MarkerSize', 4)
end
It will plot dots so make sure you have enough points to make the dots be adjacent and look like a line. You can adjust the MarkerSize if you need to.
  3 件のコメント
Image Analyst
Image Analyst 2020 年 7 月 18 日
I don't really care what the range of r is. Whatever it is, we need to scale it to the number of rows in the colormap to make it easy to pick a color for that radius. So if you have 1000 dots, and you have a r that's half way out to the max r (be it 0.75 or whatever, we don't care), then the color will be the color at half of the 1000 rows, or the color at row 500, whatever that color may be.
H-M
H-M 2020 年 7 月 18 日
Thanks for GREAT explanation.

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2020 年 7 月 18 日
編集済み: Bruno Luong 2020 年 7 月 18 日
% Generate 20 functions
x=linspace(0,2*pi,200)';
A=linspace(0.2,1,20);
y=A.*(sin(x).*exp(-x/2));
r=sqrt((x-pi).^2+(y).^2);% encoding color as circle centered at (pi,0)
% Function from https://www.mathworks.com/matlabcentral/fileexchange/60402-plotcolor
plot_color(x,y,r,...
jet,[0 3]) % <- colormap and range
Result

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by