determine the color in contour plot of a matrix in MATLAB by the size of element

StackOverflow https://stackoverflow.com/questions/20218765

  •  05-08-2022
  •  | 
  •  

سؤال

It is necessary for me to plot by contour plot in MATLAB just with 2 color by consideration of the size of the element of z matrix.

By contourf I could do this but I do not know how plot it by the size of element. for example I wanna if z_ij be grater than 0.5 the color be green and else be yellow. How could I do this?

I am grateful to you for your helps.

هل كانت مفيدة؟

المحلول

You can create a colormap, containing only 2 colors green and yellow. Then apply it for the figure using the colormap(cmap) function. The center the CLim property for the current axes around 0.5. Try to run following example,

[X,Y,Z] = peaks(30);
figure;
contourf(X,Y,Z);
cMap = [0 1 0;1 1 0]; % [green;yellow] on rgb-color
colormap(cMap);
set(get(gcf,'CurrentAxes'),'CLim',[-2 3]);
colorbar;

if you want to adjust the scale so that the max and min is the max and min of your values it is a Little bet more complicated. One way to solve it could be to get the colorbar handles and change the YTickLabel manually (or rather by creating code that sets the tick label). Or setting the CLim so high that you can manually change the YLim on the colorbar without leaving the colored interval (eg. max(abs(data-0.5)) which let you decrase one side of the YLim). Try,

[X,Y,Z] = peaks(30);
figure;
contourf(X,Y,Z);
cMap = [0 1 0;1 1 0]; % [green;yellow] on rgb-color
colormap(cMap);
set(get(gcf,'CurrentAxes'),'CLim',[-2 3]);
h = colorbar;
set(h,'YLim',[-2 1]);
pause on;
pause;
pause off;
set(h,'YLim',[-3 4]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top