Get ticks to lay across colorbar

6 次查看(过去 30 天)
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';});

采纳的回答

Dave B
Dave B 2021-9-12
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';});

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by