How to do combination of color in a single colorbar in a scatter plot?

4 vues (au cours des 30 derniers jours)
Avijit Paul
Avijit Paul le 15 Mar 2025
Commenté : Voss le 16 Mar 2025
I have one data set of 4964 values "s" varible with latitude and longitude. I want to plot all the positive values with red color (gradually shaded color) using "BREWERMAP Function" link provided. Zero Values in Green color and negative values in blue (Not shaded fix color). Tried many things couldn't able get a desired output. The figure should have a single colorbar. Tried using different axis, but that generate 3 colorbar. Any help will be appreciated.
Code
scatter(lon,lat,6,s,"filled");

Réponse acceptée

Voss
Voss le 15 Mar 2025
Maybe something like this.
load Data
reds = brewermap([],'Reds');
green = [0 1 0];
blue = [0 0 1];
smax = max(s(:));
nr = size(reds,1);
ng = 2;
nb = floor(nr/25);
c = zeros(numel(s),3);
idx = s > 0;
c(idx,:) = reds(ceil(s(idx)*nr/smax),:);
idx = s == 0;
c(idx,:) = repmat(green,nnz(idx),1);
idx = s < 0;
c(idx,:) = repmat(blue,nnz(idx),1);
scatter(lon,lat,6,c,"filled");
colormap([repelem([blue; green],[nb,ng],1); reds])
clim([-(nb+ng/2)/(nr+ng/2),1]*smax)
colorbar()
  2 commentaires
Avijit Paul
Avijit Paul le 16 Mar 2025
Thanks a lot, Perfect, was trying a lot for this output.
Voss
Voss le 16 Mar 2025
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by