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

58 vues (au cours des 30 derniers jours)
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);

Réponse acceptée

Star Strider
Star Strider le 6 Oct 2022
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 commentaires
Giuseppe
Giuseppe le 6 Oct 2022
Modifié(e) : Giuseppe le 6 Oct 2022
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 le 6 Oct 2022
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
.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by