Change interval of color bar in contour scatter plot ?

21 ビュー (過去 30 日間)
Teerapong Poltue
Teerapong Poltue 2021 年 2 月 1 日
コメント済み: Teerapong Poltue 2021 年 2 月 4 日
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 件のコメント
Teerapong Poltue
Teerapong Poltue 2021 年 2 月 2 日
Oh, sorry for that I put the wrong code in the topic.
I use the scatter plot for this (Example 3 in https://www.mathworks.com/help/matlab/ref/scatter.html).
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')
Then how can I change the color bar range for this code.
I would like to have some how like Levels of [1:2:10]
Adam Danz
Adam Danz 2021 年 2 月 3 日
See my answer below.

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

採用された回答

Adam Danz
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)
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])
  1 件のコメント
Teerapong Poltue
Teerapong Poltue 2021 年 2 月 4 日
Thanks for that, very appreciated.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by