Get ticks to lay across colorbar

24 ビュー (過去 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 Center および File Exchange2-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