How to properly set ticks, numbers and the their decimal places on colorbar?

95 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2022 年 10 月 6 日
コメント済み: Star Strider 2022 年 10 月 6 日
I want to show on the colorbar 50 ticks but visualize only 11 or 10 numbers asscoiated to them and then set 6 decimal places for each number. Can you help me?
Here is my attempt code but only one number seems to appear on the bar.
load JC_L1
load PLOs_filt
fig = figure;
color_map_L1 = flip(turbo(numel(JC_L1))); % I prefer to associate the red to high-energy level
for k1 = 1:50
plot([PLOs_filt(1).prop(k1).orbits.x],[PLOs_filt(1).prop(k1).orbits.y],...
"Color",color_map_L1(k1,:)); hold on
end
axis equal
colormap(color_map_L1);
% Colorbar settings
cb = colorbar;
ticks = flip(JC_L1)'; %Specify how many numbers show on the colorbar
set(cb,'ytick', ticks/max(ticks));
set(cb,'yticklabel',ticks);
tix = cb.Ticks; % Get Tick Values
cb.TickLabels = compose('%9.6f',tix);

採用された回答

Star Strider
Star Strider 2022 年 10 月 6 日
Try something like this —
[X,Y,Z] = peaks(50);
figure
surf(X, Y, Z)
grid on
colormap(turbo)
hcb = colorbar;
% get(hcb)
tix = hcb.Ticks; % Get Tick Values
hcb.TickLabels = compose('%9.6f',tix); % Set Tick Labels
The format descriptor '%9.6f' allows for the decimal and sign position so all the tick labels are aligned appropriately.
.
  6 件のコメント
Giuseppe
Giuseppe 2022 年 10 月 6 日
編集済み: Giuseppe 2022 年 10 月 6 日
Ok, last thing: Can you reverse the colorbar colors (from blue to red) and show the numbers containend in JC_L1 with six decimal places?
load JC_L1
format long
JC_L1'
ans = 50×1
3.000900891023230 3.000894276927840 3.000887643313580 3.000881028967010 3.000874419173230 3.000867791975870 3.000861196034850 3.000854592397690 3.000847948043080 3.000841330136040
So I need to start from the bottom of colorbar with the value 3.000577 (blue color) up to the last value 3.000901 (red color)
Star Strider
Star Strider 2022 年 10 月 6 日
Reversing the colorbar is straightforward. This also reverses the tick labels, however if you want them as they were before the colorbar was reversed, change the relevant assignment to:
cb.TickLabels(tiklblidx) = compose('%9.6f', JC_L1(flip(tiklblidx))); % Set & Format Tick Labels
That will reverse the order of the tick labels without re-flipping the colorbar. In my code they are unchanged (unflipped), so flip the index if you want to.
This now uses a select subset of ‘JC_L1’ for the colorbar tick labels.
Try this —
LD1 = load(websave('JC_L1','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1147800/JC_L1.mat'));
JC_L1 = LD1.JC_L1;
LD2 = load(websave('PLOs_filt','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1147805/PLOs_filt.mat'));
PLOs_filt = LD2.PLOs_filt;
fig = figure;
color_map_L1 = flip(turbo(numel(JC_L1))); % I prefer to associate the red to high-energy level
for k1 = 1:50
plot([PLOs_filt(1).prop(k1).orbits.x],[PLOs_filt(1).prop(k1).orbits.y],...
"Color",color_map_L1(k1,:)); hold on
end
axis equal
colormap(color_map_L1);
% Colorbar settings
cb = colorbar;
ticks = flip(JC_L1)'; %Specify how many numbers show on the colorbar
% ticklims = [min(ticks); max(ticks); min(ticks)/max(ticks); numel(ticks)] % Interest Only - Not Required For Code (Delete If Desired)
% set(cb,'ytick', ticks/max(ticks));
% set(cb,'yticklabel',ticks);
tix = cb.Ticks; % Get Tick Values
tikpos = linspace(min(tix), max(tix), numel(ticks)); % Tick Positions On 'colorbar'
cb.Ticks = tikpos; % Set Tick Positions
% tix = cb.Ticks; % Get Tick Values
cb.Direction = 'reverse';
cb.TickLabels = cell(50,1);
tiklblidx = [1 5 10 15 20 25 30 35 40 45 50];
cb.TickLabels(tiklblidx) = compose('%9.6f', JC_L1(tiklblidx)); % Set & Format Tick Labels
.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by