How to make the color gradient gradual. In the plot

How to make the colour gradient gradual. Note I want to use two colours -ve values in blue gradaul varies with values similarly for the +ve values with red .Data set is attached. I have tried it by grouping, it is working but not statisfied.

2 Comments

Is this red-to-blue color transition scheme good enough to represent contour levels in the Indian subcontinent?
My primary and only aim is to get 2 smooth gradient of two contrast colours to distinguish between negative and positive values. Have to plot it to see how it looks. What is the colormap scheme ? Thanks

Sign in to comment.

 Accepted Answer

I'm going to ignore the fact that using a smooth colorbar with discrete-valued data makes the plot unreadable. If you just want a colormap generator that sweeps between the four given colors, then here it is.
load India_data.mat
CT = bluered(10); % any even integer
scatter(lon,lat,20,rain,"filled");
xticks([66 74 82 90 98]);
xticklabels([66 74 82 90 98]);
ylim([6.5 39.5]);
yticks([8 14 20 26 32 38]);
colormap(CT); % C contains the 4 colors of choice
d = colorbar;
clim([-50 50])
%d.Ticks = linspace(1, 4, 9);
%d.TickLabels=["-40","","-20","","0","","20","","40"];
ylabel("Latitude","FontSize",16);
xlabel("Longitude","FontSize",16);
%ylabel(a,'RMSE','FontSize',16);
title('SSP126 2041-2070','FontSize',18);
The lack of contrast between the given colors isn't helping with readability, but that's what's given. You could use different colors.
CT = bluered_hicont(10); % any even integer
scatter(lon,lat,20,rain,"filled");
xticks([66 74 82 90 98]);
xticklabels([66 74 82 90 98]);
ylim([6.5 39.5]);
yticks([8 14 20 26 32 38]);
colormap(CT); % C contains the 4 colors of choice
d = colorbar;
clim([-50 50])
%d.Ticks = linspace(1, 4, 9);
%d.TickLabels=["-40","","-20","","0","","20","","40"];
ylabel("Latitude","FontSize",16);
xlabel("Longitude","FontSize",16);
%ylabel(a,'RMSE','FontSize',16);
title('SSP126 2041-2070','FontSize',18);

2 Comments

Thanks a lot. Thats whats Iam looking into.
You might also try looking on the File Exchange. There are many other similar diverging colormap generators.

Sign in to comment.

More Answers (1)

The number of colors depends on the number of unique values in your data as well as the number of unique colors in your colormap.
Does this require the Mapping Toolbox? How did you plot this data?
It looks like your data has (perhaps) only 4 unique values. If so, you can get more by assigning the data to a digital image (matrix) and then use conv2 or imfilter to blur the image. Then create a colormap with more steps in it.
% Blur image
windowWidth = 5;
kernel = ones(windowWidth) / windowWidth^2;
blurredImage = conv2(yourImage, kernel, 'same');
% Create color map
numColors = 256;
redRamp = 1 : numColors;
blueRamp = numColors : -1 : 1;
greenRamp = zeros(numColors, 1);
cmap = [redRamp(:), greenRamp, blueRamp(:)];
% Display blurred image with colormap.
imshow(blurredImage, []);
colormap(cmap);
colorbar;

5 Comments

Thanks for the reply. I have shared the data which have the original data set which contents many 4964 different values. I have converted the original data set into 4 groups therefore it contains 4 unique values. The shared plot is just the sample plot that I have plotted. Kindly request to you to try with original data. Thanks again
Can't you do that? If you want more colors, then why choose just 4 categories? Why not use all the original data?
How did you plot it with the colormap you're currently using?
lat: [8.25 8.25 8.25 8.25 8.5 8.5 8.5 8.5 8.5 8.5 8.75 8.75 8.75 8.75 8.75 8.75 8.75 9 9 9 … ] (1×4964 double)
lon: [77 77.25 77.5 77.75 76.75 77 77.25 77.5 77.75 78 76.5 76.75 77 77.25 77.5 77.75 78 … ] (1×4964 double)
rain: [-39.6030071614142 -34.1835228448315 -35.321538273426 -44.9556236112198 … ] (1×4964 double)
How can you have negative rain?
1. Rain is just variable name.
2. I have tried with original data set, problem is that it is diffcult to distinguisg between -ve and +ve values. Therefore I want to use 2 contrast colors to distinguish between the -ve and +ve values. I just want two categories of colors but in gradient form.
"I just couldn't able to do the gradient part with 2 colors groups using the original data set".
How did you plot it with the colormap you're currently using?
First I grouped the data into 4 groups. As shown in the colobar bar (0 to 20)(>20)(-20to 0)(<-20).
figure(1);
scatter(lon,lat,8,group_rain,"filled");
xticks([66 74 82 90 98]);
xticklabels([66 74 82 90 98]);
ylim([6.5 39.5]);
yticks([8 14 20 26 32 38]);
colormap(c); % C contains the 4 colors of choice
d=colorbar;
d.Ticks = linspace(1, 4, 9);
d.TickLabels=["-40","","-20","","0","","20","","40"];
ylabel("Latitude","FontSize",16);
xlabel("Longitude","FontSize",16);
%ylabel(a,'RMSE','FontSize',16);
title('SSP126 2041-2070','FontSize',18);
Negative and Positive Values

Sign in to comment.

Asked:

on 16 Dec 2023

Commented:

DGM
on 16 Dec 2023

Community Treasure Hunt

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

Start Hunting!