How to designate color in contour plot with specific values

22 vues (au cours des 30 derniers jours)
Sreeparna Majee
Sreeparna Majee le 25 Août 2020
How do I color the contour plot black for values of a specific number or higher and lower values will be white?

Réponse acceptée

Adam Danz
Adam Danz le 25 Août 2020
Modifié(e) : Adam Danz le 26 Août 2020
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 commentaire
Sreeparna Majee
Sreeparna Majee le 7 Oct 2020
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.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 25 Août 2020
[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 commentaires
KSSV
KSSV le 7 Oct 2020
You dont define c, h; this will be the output of the contour function based on your levels.
Sreeparna Majee
Sreeparna Majee le 8 Oct 2020
See I have values in my data plot. I have to assign colors to respective values as told above. How to define that?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by