Can I set explicit plot colors for specific values or value ranges

32 views (last 30 days)
I am creating a sinlge figure output containing multiple plot elements:
For the surface plot, if the value is < 1, I want it to plot in green, if the value is >= 1 I want it to plot in blue.
For the contour3 plot, if the value is >= 25, I want it to plot in red
For the scatter plot, I want the default color of blue for the circles.
Is this possible?

Accepted Answer

ChristianW on 11 Feb 2013
Yes its possible, only for contour3 its a bit fumbling.
Surface function has an input for the colormatrix C. C is referring to the colormap. Example:
P = peaks;
C(P<1) = 1; % 1 means first color in colormap
C(P>=1) = 2;
C = reshape(C,size(P));
cmap = [0 1 0; 0 0 1]; % check colormap: colormap(cmap); pcolor(cmap)
figure; surface(peaks,C); view(-35,45)
This contour example checks where the limit value is located in the colormap. Then defining all colors from that location to the end as RED.
figure; contour3(P,40); grid off; view(-25,35)
red_limit = 2;
cmap_contour = jet;
cl = caxis;
I_red = ceil((red_limit-cl(1))/range(cl)*size(cmap_contour,1));
% make colormap from I_red_limit to end all RED
cmap_contour(I_red:end,:) = repmat([1 0 0],size(cmap_contour,1)-I_red+1,1);
In scatter you can define it directly with the markertype input:
scatter(rand(200,1),rand(200,1),'ob') % 'ob' = circle blue
Alternatively you can search the whole figure for circle marker and than make them blue:
figure; hold on; box on
figure(gcf); pause(2)

More Answers (1)

Amit on 16 Jun 2017
Can you also interpolate the colors in between those stated values? I am struggling with this issue at the moment.



Community Treasure Hunt

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

Start Hunting!