简体   繁体   English

如何在MATLAB的子图中对齐xlabel和ylabel

[英]How to align xlabels and ylabels in subplot in MATLAB

I have the following code: 我有以下代码:

  mean_h =[11.9877,13.3937,16.1717];
   std_h = [12.5379,10.2732,10.8000];
    subplot(2,1,1)
   hold on
    h = bar(1:3,mean_h,0.2);
   errorbar(1:3,mean_h,std_h,'s','MarkerSize',10,...
   'MarkerEdgeColor','red','MarkerFaceColor','red');
   name = {'4 mics','9 mics','24 mics'};
    set(gca,'XTick',[1 2 3],'XTickLabel',{'4 mics','9 mics','24 mics'});
   set(gca,'fontsize', 21);

  legend({'mean_{hor}', 'std_{hor}'});
  grid on
  xlabel(' 3 different subsets of horizontal microphone pair combinations of 
   microphone array 3');
   ylabel('Mean/stds rmse`s [°]');



 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%% Mean and standard devaiation for vertical rmse's of microphone array 3
  mean_v =[7.3022,11.3737,16.2675];
 std_v =[6.2369,9.9813,10.3599];

subplot(2,1,2)
hold on
h = bar(1:3,mean_v,0.2);
errorbar(1:3,mean_v,std_v,'s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red');
name = {'4 mics','9 mics','24 mics'};
set(gca,'XTick',[1 2 3],'XTickLabel',{'4 mics','9 mics','24 mics'});
set(gca,'fontsize', 21);
 legend({'mean_{ver}', 'std_{ver}'});

  grid on
  xlabel('3 different subsets of vertical microphone pair combinations of 
  microphone array 3');
  ylabel('Mean/stds rmse`s [°]');

Now when i plot these two subplots i am facing an alignment problem in xlabels and ylabels . 现在,当我绘制这两个子图时,我面临着xlabels和ylabels的对齐问题。 They are not aligned. 它们不对齐。 Can anybody help me how can i fix this problem. 谁能帮我解决这个问题。 Thanks 谢谢

They don't align if the graphs are different. 如果图形不同,它们将无法对齐。 In your case the numbers are different. 您的情况是不同的。

You can either do it by hand or use a text(my_x, my_y,'mylabel') and set it up properly. 您可以手动完成操作,也可以使用text(my_x, my_y,'mylabel')正确设置。 I wrote an example below. 我在下面写了一个例子。

a=1:10;
b=a.^2;

subplot(4,1,1)
plot(a,b)
ylabel(' long text')

subplot(4,1,2)
plot(a,a)
ylabel('long even long text')

subplot(4,1,3)
hold
plot(a,b)
text(0,50,'long text','HorizontalAlignment','center','VerticalAlignment','middle', ...
'FontSize',12,'Rotation',90)

subplot(4,1,4)
hold
plot(a,a)
text(0,5,'long even long text','HorizontalAlignment','center','VerticalAlignment','middle', ...
'FontSize',12,'Rotation',90)

giving the graph below 给下面的图 图表

Notice the last two are aligned, but it was necessary to input the coordinates by hand as i did. 注意最后两个是对齐的,但是有必要像我一样手动输入坐标。 As a tip, you often ca use your data and move it by a fixed percentage (eg, the minimum -10% graph total length), making it possible to 'automate' your script. 提示,您经常可以使用数据并将其移动固定的百分比(例如,图形总长度的最小-10%),从而可以“自动化”脚本。

Another way is to use the Position property of the ylabel. 另一种方法是使用ylabel的Position属性。 But this is similar to the method described above. 但是,这类似于上述方法。 To do so use 为此使用

t=ylabel('long text')
t = 
  Text ( long text) with properties:

                 String: ' long text'
               FontSize: 11
             FontWeight: 'normal'
               FontName: 'Helvetica'
                  Color: [0.15 0.15 0.15]
    HorizontalAlignment: 'center'
               Position: [0.3333 50 -1]
    %other things here too

%overwrite the property
% Here you have to put [X_position Y_position Z_position]
t.Position= [0.2 50 -1];

For that to work, every x/ylabel need its own name, or same name, but update the position before you call the command again. 为此,每个x / ylabel都需要使用自己的名称或相同的名称,但是在再次调用该命令之前更新位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM