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

1 回表示 (過去 30 日間)
Avijit Paul
Avijit Paul 2025 年 3 月 15 日
コメント済み: Voss 2025 年 3 月 16 日
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");

採用された回答

Voss
Voss 2025 年 3 月 15 日
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 件のコメント
Avijit Paul
Avijit Paul 2025 年 3 月 16 日
Thanks a lot, Perfect, was trying a lot for this output.
Voss
Voss 2025 年 3 月 16 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColorbar についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by