Change interval of color bar in contour scatter plot ?
23 次查看(过去 30 天)
显示 更早的评论
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
scatter(x,y,[],c,'s','Fill')
How can I change the contour and colorbar interval to be a certain value [1:2:10]
5 个评论
采纳的回答
Adam Danz
2021-2-2
> 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();
caxis([0,10])
set(cb, 'Ticks', 0:2:10)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/507605/image.png)
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
figure()
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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/507610/image.png)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!