Color map from green to red

A
Audrey · Nov 3, 2020 · 2.1K views
Question
I have a map with values and i would like to display the values with colors - from green to red. The value closest to 0 will be green and the farthest will be red.     Example: 1, 5, 11, 33, 56, 100 1 - Green. 100 - Red. Can be also: -5, -23, -43, -55, -80. -5 - Green. -80 - Red. The rest values will be in the order and color bar.
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 11, 2026
You can create your original colormap (green to red) and apply to the data.
The following is an example:
 
% Create green-to-red colormap
cMap = interp1([0;1],[0 1 0; 1 0 0],linspace(0,1,256));

% Apply to the plot
surf(peaks)
colormap(cMap)
colorbar

 

Creating a colormap that transitions from green to red in MATLAB is a great way to visualize your data. Here's how you can do it:

1. Generate Your Data:
   Assume you have a matrix `data` that contains your values.

   data = rand(10, 10);  % Replace this with your actual data
 

2. Create the Colormap**:
   Define a colormap that transitions from green to red.

   
   colormap = [linspace(0, 1, 256)', linspace(1, 0, 256)', zeros(256, 1)];
  

3. Display the Data:
   Use the `imagesc` function to display the data and apply the colormap.


   imagesc(data);
   colormap(colormap);
   colorbar;
 

4. Adjust Color Limits:
   Optionally, you can adjust the color limits to ensure that the value closest to 0 is green and the farthest value is red.

 
   caxis([min(data(:)), max(data(:))]);
   

Here’s the complete code:

data = rand(10, 10);  % Replace this with your actual data
colormap = [linspace(0, 1, 256)', linspace(1, 0, 256)', zeros(256, 1)];
imagesc(data);
colormap(colormap);
colorbar;
caxis([min(data(:)), max(data(:))]);

This will create a heatmap with colors ranging from green (for values closest to 0) to red (for the highest values).

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!