How do I plot a sine function with each point having a z variable
조회 수: 2 (최근 30일)
이전 댓글 표시
I would like to plot say a sine function for which I have created two arrays, x and y, where y=sine(x). Now each point (x,y) has an associated z variable defined in a third array z. I would like to plot y with a heat map such that various points on the sine function have different colors based on the z value and the rest of the plot is not colored. An example of such a plot is attached.
I tried imagsc(x,y,z) but that did not work. Any hints would be appreciated.
Vahid
댓글 수: 1
dpb
2016년 11월 13일
편집: dpb
2016년 11월 13일
In Matlab HG, lines are a single object and have only a single color. To draw varied-colored "lines" simulating the above plot would entail building an image similar as your initial trial or with HG using scatter with solid markers in close proximity to simulate a line or very short individual line segments with a color map with each (essentially using plot|line as a scatter substitute).
Would make a reasonable (and useful) enhancement request...
채택된 답변
dpb
2016년 11월 12일
편집: dpb
2016년 11월 13일
scatter(x,y,5,z)
hold on
plot(x,y,'k-')
latter two lines if want the line between points; otherwise forget it...
ADDENDUM Actually, the scatter solution works reasonably well...
>> N=1000;
>> x=linspace(0,2*pi,N);
>> y=sin(x);
>> z=linspace(-1,1,length(x));
>> scatter(x,y,15,z,'o','filled')
>>
results in:

You can 'spearmint w/ how few points can get away with and whether the number of scatter object handles becomes excessive for a plot as complex as that you show.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!