![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175722/image.png)
How can I plot a graph where point colour relies on its value, based on several greater than and less than rules?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ryan Hickson
am 27 Apr. 2017
Beantwortet: Image Analyst
am 27 Apr. 2017
I am trying to plot graphs where the points against the Y-axis determine the point itself to be one of five or more colours based on rules such as <0, >0 & <5, >5 & <42 etc.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 27 Apr. 2017
Here's one way:
y = 10*rand(1, 150);
myColorMap = zeros(length(y), 3);
for k = 1 : length(y)
if y(k) < 3
myColorMap(k, :) = [1,0,0]; % Red
elseif y(k) < 6
myColorMap(k, :) = [1,0,1]; % Magenta
else
myColorMap(k, :) = [0,0,1]; % Blue
end
end
x = 1:length(y);
scatter(x, y, 20, myColorMap, 'filled');
grid on;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175722/image.png)
0 Kommentare
Weitere Antworten (1)
Steven Lord
am 27 Apr. 2017
Use your conditions to generate a vector of values that you specify as the c input in a call to the scatter function. If your conditions are simply "In the range (a, b)" (with the possibility for one or both of the endpoints to be inclusive) consider using discretize or histcounts to generate the vector.
Siehe auch
Kategorien
Mehr zu Scatter Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!