Get ticks to lay across colorbar

6 views (last 30 days)
John Cruce
John Cruce on 12 Sep 2021
Answered: Dave B on 12 Sep 2021
I'm desiring to place ticks across my colorbar to more clearly separate the color palette. I've set the ticklength to be identical to my colorbar height, but something about my axes or figure handle (gca or gcf) is throwing the ticklength off so that the ticks don't lay across the entire colorbar. Any suggestions on how I can fix this?
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','w');
set(gcf,'units','pixel','position',[70,70,500,500],'papersize',[500,500],'color','w');
ax=gca;
colormap(jet(50));
caxis([0, 50]);
hcb=colorbar('SouthOutside');
hcb.Label.String='Test Color Bar';
axPos = ax.Position;
colorbarpos=hcb.Position;
% Position and scale for SouthOutside colorbar
colorbarpos(2)=0.032+colorbarpos(2);
colorbarpos(4)=0.5*colorbarpos(4);
hcb.Position = colorbarpos;
ax.Position = axPos;
set(hcb,'YTick',[0:1:50],'TickLength',colorbarpos(4));
set(hcb,'YTickLabel',{'';'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';});

Accepted Answer

Dave B
Dave B on 12 Sep 2021
The unit for TickLength is peculiar. If you check out how it's documented on the colorbar page it says:
Tick mark length, specified as a scalar. Specify the tick length as a fraction of the colorbar axis length.
That 'length' is sort of like the length function in MATLAB, the longer direction.
In your case I think you want TickLength to be: colorbarpos(4)/colorbarpos(3)
set(gca,'xtick',[],'xticklabel',[],'ytick',[],'yticklabel',[],'color','w');
set(gcf,'units','pixel','position',[70,70,500,500],'papersize',[500,500],'color','w');
ax=gca;
colormap(jet(50));
caxis([0, 50]);
hcb=colorbar('SouthOutside');
hcb.Label.String='Test Color Bar';
axPos = ax.Position;
colorbarpos=hcb.Position;
% Position and scale for SouthOutside colorbar
colorbarpos(2)=0.032+colorbarpos(2);
colorbarpos(4)=0.5*colorbarpos(4);
hcb.Position = colorbarpos;
ax.Position = axPos;
set(hcb,'YTick',[0:1:50],'TickLength',colorbarpos(4)/colorbarpos(3));
set(hcb,'YTickLabel',{'';'1';'';'';'';'5';'';'';'';'';'10';'';'';'';'';'15';'';'';'';'';'20';'';'';'';'';'25';'';'';'';'';'30';'';'';'';'';'35';'';'';'';'';'40';'';'';'';'';'45';'';'';'';'';'50';});

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!