How to properly set ticks, numbers and the their decimal places on colorbar?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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);

Accepted Answer
Star Strider
on 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 Comments
Hi @Star Strider, thanks for your reply. Your code works well but I realize that my code was wrong because I didn't properly set the ticks on my colorbar ((Actually, I previoulsy changed only ticks label but not the number of ticks themselves)) so I modifyed my question, I added your code fragment but there is something wrong. Can you give a loook at my modified code?
Alternatively, you can modify your example by setting 50 ticks and displaying only 10 or 11 values on the colorbar.
One problem is that your code wants to force 50 tick labels onto the colorbar. That can easily be done, however fewer would be easier to see.
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)
ticklims = 4×1
3.0006
3.0009
0.9999
50.0000
% 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.TickLabels = compose('%9.6f',ticks/max(ticks)); % Set & Format Tick Labels

% cb.FontSize = 5;
The tick positions must be in the range of the current ticks, so the ‘tickpos’ vector creates 50 of them in that range. After that, the compose call creates and formats the labels. They are automatically positioned on the previously set ticks.
To make the tick labels easier to read, I suggest creating fewer elements of the ‘ticks’ vector to begin with. The rest of the code will automatically accommodate them.
.
Giuseppe
on 6 Oct 2022
@Star Strider thank you again. I would want to avoid to set only some ticks but I would prefer to mantain all 50 ticks and I'd want to show only 10 numbers, is it possible to get this?
P.s. I tried implementing your code but the numbers on colorbar are not those of my JC_1 array.
I do not understand.
I am using the ‘ticks’ vector (derived from the ‘JC_L1’ array) to create the tick labels, because that is apparently what you use, and then divide them by the maximum (apparently to scale them). Choose whatever you want to use for the tick labels, providing that there are enough of them to create the arrays.
Creating 11 tick labels while displaying all 50 ticks is relatively straightforward, and this revised code does that. Change the values in ‘tiklblidx’ to control the tick values displayed, and where they are displayed. They are indices, so they must be integers greater than 0 and between 1 and 50.
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.TickLabels = cell(50,1);
tiklblidx = [1 5 10 15 20 25 30 35 40 45 50];
cb.TickLabels(tiklblidx) = compose('%9.6f',ticks(tiklblidx)/max(ticks)); % Set & Format Tick Labels

.
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)
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

.
More Answers (0)
Categories
Find more on Colorbar in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)