Formatting Subplots in MATLAB
35 次查看(过去 30 天)
显示 更早的评论
I am trying to create a series of vertically stacked plots for a scientific publication using the same x-axis. With regards to this task, I have a couple questions.
1) Generating the subplots according to the MATLAB defaults, there is always good bit of space between each individual plot in the group. I am trying to get each plot to sit right on the top of the others, but all of the things I have tried have not worked. From looking around, I believe I need to set height space between the subplots, but I could be wrong. I need tidy way to reduce and/or eliminate the space between the subplots. Current figure and the a figure I am trying the replicate the style found below.
2) Also, does anyone know why the far right y-axis on the bottom plot does not line up with the that of the top plot? Would setting sizes for the plots in the code eliminate this problem? Current code is attached as it is pretty long.
回答(2 个)
Alvi Newaz
2016-9-14
Hi you can get the position of bottom subplot using p = get(subplot,'Position'). This is a 4-element vector [left, bottom, width, height]. So you can get the upper edge of the second plot by adding p(2) and p(4). Then start first plot from that position.
Example code:
a = [1:10];
b = [1:10];
figure(1)
h1 = subplot(2,1,1);
scatter(a,b);
ylabel('data1')
set(gca,'XTickLabel',[]);%remove Xaxis tick labels for h1
h2 = subplot(2,1,2);
scatter(a,b);
xlabel('x axis data');
ylabel('data2')
p1 = get(h1, 'Position');
p2 = get(h2, 'Position');
p1(2) = p2(2)+p2(4);%change bottom starting point of h1
set(h1, 'pos', p1);%set h1 position
0 个评论
Geoff Hayes
2016-9-14
Try manipulating the position of the second subplot. For example, you can get the current position value of the second subplot as
% get the handle to the subplot
hAxes2 = subplot(2,1,2);
% get the current position
axes2Pos = get(hAxes2, 'Position');
% move the subplot (axes) up
axes2Pos(2) = axes2Pos(2)*2;
% reset the new position
set(hAxes2,'Position',axes2Pos);
The above is just an example. The position array should be a 1x4 with the x coordinate of the bottom left corner of the axes, the y coordinate of the bottom left corner of the axes, the width of the axes, and the height. These values may or may not be normalized. As we want to move this axes up, we just increase the y-coordinate (in this case, by two).
Try the above and see what happens!
3 个评论
Geoff Hayes
2016-9-19
I can't seem to reproduce that problem with my version of MATLAB (r2014a) so perhaps the code I'm using is somewhat different from yours. Can you post a sample of code and the steps that illustrate the problem?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!