MATLAB Answers

Change interval of color bar in contour scatter plot ?

41 views (last 30 days)
Now I have my code plotting a scatter plot.
x = [1:0.1:10];
c = [10:-0.1:1];
y = zeros(1,91);
y = y - 1;
hold on
How can I change the contour and colorbar interval to be a certain value [1:2:10]

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 2 Feb 2021
> How can I change the color bar range for this code. I would like to have some how like Levels of [1:2:10]
This question is unclear and can be interpretted in several ways.
To change the color range use caxis().
To create a discrete colormap you've got several options.
If you only want n discrete colors you can use any of the colormap functions and specify the number of levels.
cmap = jet(n);
cmap = cool(n); % etc...
You can combine that with caxis to define when the discrete colors change. For example,
% Create colorbar that ranges 0:10 and changes
% colors at 0:2:10
cmap = jet(5);
set(gca, 'Colormap',cmap)
cb = colorbar();
set(cb, 'Ticks', 0:2:10)
To create a discrete colormap that indicates ranges of x-values of a scatter plot, you need to set the color input to scatter defined by the discrete ranges of x-values.
% Create scatter data
x = 1:0.1:10;
y = zeros(1,91);
% Partition x values into bins to define color
edges = min(x):2:max(x)+1;
c = discretize(x, edges);
c = edges(c);
% Plot results
ax = axes();
scatter(ax, x,y,[],c,'s','Fill')
grid(ax, 'on')
ax.XTick = edges;
colormap(ax, jet(numel(edges)-1)); % set colormap
caxis(edges([1,end])) % set color range
cb = colorbar();
cb.Ticks = edges;
xlim([min(x)-1, max(x)+1])

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by