How to properly set ticks, numbers and the their decimal places on colorbar?
58 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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);
0 commentaires
Réponse acceptée
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
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
.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!