我正在创建一个这样的条形图: 我的问题是我想要旋转刻度标签(因为它们很长)。我正在使用来自Matlab文件交换的Xticklabel_rotate函数。不幸的是,这似乎不适用于下标或上标的Tex命令,我还想为摄氏度添加度数符号。任何人都有一个旋转x-tick的解决方案,允许在Matlab中使用Tex命令?
这是我的代码:
% Necessary variables:
orderedDriver = {'T21m','Tsoil','prevNEE','vpd','h2osoil'};
tmp1 = {'T21m';'Tsoil';'prevNEE';'vpd';'h2osoil'};
B = [0.373457670910667,0.332391484167831,0.244013230957008,0.209637035240858,0.155440287941303];
r2_benchmark = [0.563879739982401];
month = ['snowmelt'];
fname='Arial';
fsize=22;
fsizeMed=18;
% start figure
scrsz = get(0,'ScreenSize');
figure1 =figure('Position',[1 scrsz(4) scrsz(3)/2 scrsz(4)/2.5]);
% make string of Xtick names
tmp1=orderedDriver(:);
XTL_prim={};
for z = 1:length(tmp1)
if strcmpi(tmp1(z),'h2osoil')
XTL_prim{z}='Soil moist. (m^3/m^3)';
elseif strcmpi(tmp1(z),'Tsoil')
XTL_prim{z}='Soil temp (C)';
elseif strcmpi(tmp1(z),'T21m')
XTL_prim{z}='Air temp. (C)';
elseif strcmpi(tmp1(z),'vpd')
XTL_prim{z}='VPD (kPa)';
elseif strcmpi(tmp1(z),'Rnet25')
XTL_prim{z}='Net rad. (W/m^2)';
elseif strcmpi(tmp1(z),'ustar21m')
XTL_prim{z}='U* (m/s)';
elseif strcmpi(tmp1(z),'swe')
XTL_prim{z}='SWE (mm)';
elseif strcmpi(tmp1(z),'wd21m')
XTL_prim{z}= 'Wind dir. (deg. from N)';
elseif strcmpi(tmp1(z),'RH2m')
XTL_prim{z}='Rel. Humidity';
elseif strcmpi(tmp1(z),'precipmm')
XTL_prim{z}='Precip (mm)';
elseif strcmpi(tmp1(z),'ws21m')
XTL_prim{z}='Wind speed (m/s)';
elseif strcmpi(tmp1(z),'Rppfdin')
XTL_prim{z}='In. PAR (umol/m^2/s)';
elseif strcmpi(tmp1(z),'prevNEE')
XTL_prim{z}='Previous NEE'; % ADD PREVIOUS NEE units
end
end
% Create axes and XTick properties
axes1 = axes('Parent',figure1,...
'XTickLabel',XTL_prim,...
'XTick',1:length(orderedDriver),'FontSize',fsizeMed,'Position',[0.2 0.25 0.671 0.659]); %Play with [x, y, xsize, ysize] to get figure positioned well
hold(axes1,'all');
% hold on
eachH=bar(B);
xlim([0 length(orderedDriver)+1])
ylim([0 1])
% Change bar color
set(eachH,'facecolor',[0.4 0.4 0.4])
% plot the best we can expect from the benchmark
l1=line([0;length(orderedDriver)+1],[r2_benchmark;r2_benchmark]);
set(l1,'LineStyle','--','color','k')
% Rotate labels
xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','none')
% Label axes and add title
ylabel('r^2')
xlabel('Individual Drivers','FontName',fname)
hold off
text(1,0.93,month,...
'FontSize',fsizeMed); % can use 'title' command or simply text for title
答案 0 :(得分:1)
我建议您使用format_tick
代替xticklabel_rotate
,前者允许您将标签格式化为TeX对象,并且还有定位选项。
在代码中进行以下更改以获得所需的输出:
更改自:
% Rotate labels
xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','none')
为:
% Rotate labels
format_ticks(gca, XTL_prim, [], [], [], 45)
设置xlabel
后添加:
xlabh = get(gca,'XLabel');
set(xlabh,'Position',get(xlabh,'Position') - [0 0.25 0])
结果:
如果您认为它们距离条形太近,您可以在format_tick
中指定刻度标签的位置。要添加学位:'Soil temp (C\circ)'
。
答案 1 :(得分:1)
我发现了另外两种解决方案。一个是替换
xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','none')
与
xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','tex')
(我本可以发誓我已经尝过了!)
另一种是使用最新版本的Matlab(R2014b),它具有新的命令可能性'XTickLabelRotation'。
% Create axes and XTick properties
axes1 = axes('Parent',figure1,...
'XTickLabel',XTL_prim,...
'XTick',1:length(orderedDriver),...
'XTickLabelRotation',45,...
'FontSize',fsizeMed,'Position',[0.2 0.25 0.671 0.659]); %Play with [x, y, xsize, ysize] to get figure positioned well
hold(axes1,'all');