rotate colorbar tick labels

23 ビュー (過去 30 日間)
mkarikom
mkarikom 2020 年 5 月 19 日
コメント済み: Walter Roberson 2024 年 11 月 14 日 3:33
I have the following which generates a colorbar. However, the orientation of the tick labels means that if the font is increased they will run together. Thus I need to rotate the tick lables in order to make them bigger *and* still legible
figure;
tiledlayout(1,1);
skipticks = 3;
t1=nexttile;
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
offset = rand(1)*1e-6;
Z = Z*1e-14 + offset;
pcolor(X,Y,Z)
cmorig = colormap(gca);
view(2)
colorbar('southoutside');
Result:
What I need:

回答 (2 件)

Tommy
Tommy 2020 年 5 月 20 日
You could place the labels yourself using text. Personally, I'd rather MATLAB figure out the placement.
Here's code which puts a set of invisible axes on top of the colorbar, turns the colorbar tick labels off, and instead shows labels for the axes ticks. The axes ticks are placed where labels previously existed in the colorbar. Then xtickangle rotates the labels.
figure;
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
offset = rand(1)*1e-6;
Z = Z*1e-14 + offset;
pcolor(X,Y,Z)
cmorig = colormap(gca);
view(2)
% changes/additions start here
cb = colorbar('southoutside');
keepLabels = ~cellfun(@isempty,cb.TickLabels);
cb.TickLabels = [];
ax = axes('Position', cb.Position,...
'Color', 'none',...
'YTick', [],...
'XLim', cb.Limits,...
'XTick', cb.Ticks(keepLabels),...
'FontSize', cb.FontSize);
xtickangle(ax, -30);
The result is this:
Of course repositioning the colorbar will mess it up. You may need to manually reposition some things yourself to keep the 'x 10^-n' within the figure.
This may be overcomplicated, but I wasn't sure how else to do it without placing the labels yourself. Hopefully this works for you!

qizhi yang
qizhi yang 2024 年 11 月 14 日 2:58
Cb.Ruler.TickLabelRotation=40
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 11 月 14 日 3:33
This is the way, when it is understood that
Cb = colorbar('southoutside');
The one small thing: the TickLabelRotation should be set to roughly -45 to match the original request.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by