Setting Zero to a specific color in 3D surface plot

I am trying to set the color scale for my data, which blue for the positive, red for the negative, and setting zero as grey color.
I had been using the code now, which seperate the positive and negetive, but not the zero yet.
%data here means the data I imported from the excel sheet.
y = data(2:end,1) ;
x = data(1,2:end) ;
Z = data(2:end,2:end) ;
surf(x,y,Z)
colormap(hot(2))
shading interp
caxis([-15 15])
I tried to remove the zero by setting zero as NaN, but this affects the result as "interp" is used.
Is it posible to set the zero as as specifc color with this condition?

 채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 2일
y = data(2:end,1) ;
x = data(1,2:end) ;
Z = data(2:end,2:end) ;
C = sign(Z);
warp(x, y, Z, C)
caxis([-1 1])
colormap([1 0 0;0.2 0.2 0.2;0 0 1])
I did not use surf() because surf() extrapolates between vertices, so the color at each face would reflect the average of adjacent nodes, whereas with warp() it should be point by point

댓글 수: 4

kenneth
kenneth 2022년 8월 2일
편집: kenneth 2022년 8월 2일
For this solution, the black color is within the range of nearly [-0.2 , 0.2]. Any method to make the black color range to be only for 0?
The color is based on sign(Z) which is only zero when Z is exactly 0.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1085635/New%20Microsoft%20Excel%20Worksheet.xlsx';
data = readmatrix(filename);
y = data(2:end,1) ;
x = data(1,2:end) ;
Z = data(2:end,2:end) ;
C = sign(Z);
warp(x, y, Z, C)
caxis([-1 1])
colormap([1 0 0;0.4 0.4 0.4;0 0 1])
When I probe that with the data cursor, the gray part is all exactly 0.
Oh, thanks for the solution, now I understand the concepts. Thanks so much, I had been looking for this solution for the whole day. Really appreciate that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2022년 8월 2일

댓글:

2022년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by