MATLAB Answers

create specific color map and save it

3 views (last 30 days)
Sajid Afaque
Sajid Afaque on 28 May 2020
Commented: Ameer Hamza on 28 May 2020
how do i create my own color map for 2D-scatter plot.
such that all the values of z
  1. z <= 10 ------- take green color
  2. 10 < z <= 25 ----- yellow color
  3. 25 < z <= 50 ----- orange color
  4. z > 50 ----- red color
i want to create a color map obeying such condition and save it , so that i can use with whatever z i obtain in future.
as an example i have attached x,y,z
scatter(x,y,[],z,'filled','o')

  1 Comment

KSSV
KSSV on 28 May 2020
Same question you have asked multiple times...

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 28 May 2020
Try this
val_z = (10:51).';
idx = 1*(val_z<=10) + ...
2*((10<val_z) & (val_z<=25)) + ...
3*((25<val_z) & (val_z<=50)) + ...
4*(val_z>50);
colors = [0 1 0; % green
1 1 0; % yellow
1 0.5 0; % orange
1 0 0]; % red
cmap = colors(idx, :);
scatter(x,y,[],z,'filled','o')
colormap(cmap)
set(gca, 'CLim', [10 51]);

  12 Comments

Show 9 older comments
Ameer Hamza
Ameer Hamza on 28 May 2020
There seems to be some difference in the working of colormap in R2013b. In R2020a, it looks fine
Sajid Afaque
Sajid Afaque on 28 May 2020
yeah color ranges are ok ,only colorbar gets repeated after certain interval.
do you have any idea why this may be.
thank you for your help .
much appreciated
Ameer Hamza
Ameer Hamza on 28 May 2020
I don't have any idea about this. Maybe something is mentioned in archived documentation, but the reason is not obvious.

Sign in to comment.

More Answers (0)