Get ticks to lay across colorbar

조회 수: 22 (최근 30일)
John Cruce
John Cruce 2021년 9월 12일
답변: Dave B 2021년 9월 12일
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에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by