Does MATLAB have a nonlinear colormap? How do I make one?

18 views (last 30 days)
I have data mostly between 1 and 10, but with a small percentages of values from 30 to 180. The problem is that every MATLAB colormap is linear, such that nearly all of my data looks the same, and only the extreme values show contrast. In addition, don't want to do something like take the logarithm of my data, as the raw data values are very important to my analysis.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Sep 2016
It sounds like you would like to plot your data to have greater color variation in a certain range, but would prefer not to transform your data. For a task like this, I would suggest creating your own nonlinear colormap. I have constructed an example below which you may be able to adapt to your code and data. 
Note that you could also change how your colorbar displays, but I have focused on the colormap itself.
First, I’d pick an existing colormap, such as Parula. Specify the max and min values of your data (e.g. 34 and -350), and then select the value at which you would like more color variation (e.g. perhaps 34 or 0). You can play with the scaling intensity parameter to see what looks nice.
 
  cMap = parula(256);
  dataMax = 192;
  dataMin = 1;
  centerPoint = 1;
  scalingIntensity = 5;
Then perform some operations to create your colormap. I have done this by altering the indices “x” at which each existing color lives, and then interpolating to expand or shrink certain areas of the spectrum.
  x = 1:length(cMap); 
  x = x - (centerPoint-dataMin)*length(x)/(dataMax-dataMin);
  x = scalingIntensity * x/max(abs(x));
Next, select some function or operations to transform the original linear indices into nonlinear. In the last line, I then use “interp1” to create the new colormap from the original colormap and the transformed indices.
  x = sign(x).* exp(abs(x));
  x = x - min(x); x = x*511/max(x)+1; 
  newMap = interp1(x, cMap, 1:512);
Then plot! 
  load cape;
  figure; imagesc(X);
  figure; imagesc(X); colormap(newMap);
 
  1 Comment
Walter Roberson
Walter Roberson on 30 Oct 2017
In R2014a and earlier, colorbar() created an axes, and it was possible to set the YScale for the axes to 'log' in order to change the visual representation of what was being drawn.
However, in R2014b and later, colorbar() now returns a colorbar object, which uses a java object to do the drawing, and that java object has no obvious way of switching to log space. The colorbar object does have a DataSpace.YScale property that looks like it should be the appropriate item to change, but changing it to 'log' is ignored.

Sign in to comment.

More Answers (0)

Categories

Find more on Color and Styling in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!