How to designate color in contour plot with specific values

조회 수: 21 (최근 30일)
Sreeparna Majee
Sreeparna Majee 2020년 8월 25일
댓글: Sreeparna Majee 2020년 10월 8일
How do I color the contour plot black for values of a specific number or higher and lower values will be white?

채택된 답변

Adam Danz
Adam Danz 2020년 8월 25일
편집: Adam Danz 2020년 8월 26일
Here's a demo.
  1. Produce the contour plot.
  2. Set the binaryThreshold variable that defines the border of white/black
  3. Redefine the colormap
% Create contour plot
Z = peaks;
[~,c] = contourf(Z);
cb = colorbar();
% Set binary threshold to separate the two colors.
% See c.LevelList for list of contour levels, although you can
% set this value to any value within the range of levels.
binaryThreshold = 2.0;
% Determine where the binary threshold is within the current colormap
% All changes to contour plot should be made prior to this.
ax = gca();
crng = caxis(ax); % range of color values (same as min|max of c.LevelList)
clrmap = ax.Colormap;
nColor = size(clrmap,1);
binThreshNorm = (binaryThreshold - crng(1)) / (crng(2) - crng(1));
binThreshRow = round(nColor * binThreshNorm); % round() results in approximation
% Change colormap to binary
% White section first to label values less than threshold.
newColormap = [ones(binThreshRow,3); zeros(nColor-binThreshRow, 3)];
ax.Colormap = newColormap;
% Add gray contour line labels
c.LineColor = [.5 .5 .5];
c.LineWidth = 2;
% Add frame to colorbar
cb.LineWidth = .7;
Note that since all colormaps have a discrete number of colors, the border between black & white is very closely approximated and will most likely not cause a problem. For example, with a colormap of 256 colors, if the threshold is set to 33% of the color range, the transition would happen between color #84 and #85 which would be rounded to 84. If this poses a problem, you can either slightly reduce or increaes the threshold or you can change round() to ceil() or floor().
  댓글 수: 1
Sreeparna Majee
Sreeparna Majee 2020년 10월 7일
Thank you for the solution. It is working when I want a certain percentage of a contour plot to be black or white. But what would be the case if I want for specific values? For ex: I want white for zero values and black for all other values. just the way in data file, no percentage.

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

추가 답변 (1개)

KSSV
KSSV 2020년 8월 25일
[c,h] = contour()
c has all the points and h has all the repsective levels. Pick your required level and use plot with the color you want.
  댓글 수: 3
KSSV
KSSV 2020년 10월 7일
You dont define c, h; this will be the output of the contour function based on your levels.
Sreeparna Majee
Sreeparna Majee 2020년 10월 8일
See I have values in my data plot. I have to assign colors to respective values as told above. How to define that?

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

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by