![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
Custom colors on plots
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, i'm trying to plot:
x=[1,2,3,4,5,6,7,8,9];
y=[9,8,7,6,5,4,3,2,1];
which gives me a straight line,
I also have a third array
height=[0,0,2,3,0,0,2,1,0];
I want to plot markers on the plot above whenever height(i) is not equal to 0.
I can do this by using:
plot(xVal,yVal,'ks','markerfacecolor',[0 0 0]);
however this only works for black/default colours. I want to be able to have different shades of blue for each marker depending on how large height(i) is for each point.
How can I get custom colors?
댓글 수: 0
채택된 답변
Image Analyst
2013년 9월 28일
You can plot them one at a time:
x=[1,2,3,4,5,6,7,8,9];
y=[9,8,7,6,5,4,3,2,1];
height=[0,0,2,3,0,0,2,1,0];
heightDifferences = abs(height - y);
% Find the max height difference.
% This will be put blue. Others will be less blue.
% I.e., on line = black, furthest away = pure blue.
maxHeightDiff = max(heightDifferences(height~=0));
plot(x,y,'ks','markerfacecolor',[0 0 0]);
plot(x,y,'rd-');
grid on;
hold on;
for k = 1 : length(height)
if height(k) ~= 0
theColor = [0,0,heightDifferences(k)/maxHeightDiff];
plot(x(k), height(k), 'o', 'markerfacecolor', theColor,...
'MarkerSize', 15);
end
end
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
Or you can call scatter() and pass in the colors if you make theColor an array in the above code.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!