How to plot z component as a colorbar with lines connecting scatter plot points?

39 ビュー (過去 30 日間)
Brianna Miranda
Brianna Miranda 2022 年 3 月 30 日
回答済み: Dave B 2022 年 3 月 30 日
I have a data set with x, y and z scalar components. I am trying to make an x vs y plot with z shown as a colorbar. My current code does this however, I need it to connect all the points with a line and that line should be a colorbar. I tried adding line(x,y) to the plotting code but this adds just a line without a color scale. How do I add a line to connect (x,y) points that is a colorbar of z?
scatter(x,y,20,z);
xlabel('x');
ylabel('y');
c = colorbar;
c.Label.String = 'z';

採用された回答

Dave B
Dave B 2022 年 3 月 30 日
I think what you're asking for is a color gradient on the line? Unfortunately lines don't have color gradients. A common workaround is to use something like patch or surface which do have color gradients. To make this work you have to do a trick where you make two copies of the data, flipping one:
x=rand(10,1);
y=rand(10,1);
c=rand(10,1);
scatter(x,y,20,c,'f')
patch([x;flip(x)],[y;flip(y)],[c;flip(c)],'FaceColor','none','EdgeColor','interp')
c=colorbar;
c.Label.String='z';

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by