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 CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by