MATLAB中subplot绘图相关技巧

http://xiaotingzi.blog.163.com/blog/static/217769214201361981535727/

The first thing I want to mention is that tightfig has a different purpose than the other entries. The description on the entry page explains the purpose very well: "Alters a figure so that it has the minimum size necessary to enclose all axes in the figure without excess space around them." Note that this is about making the outer bounds of the figure tight. Its intention is not to modify any of the spacings between the axes, which is the primary purpose of the other entries. I'll say more on this later.

figure('Color', [.8 .8 .8]);

subplot(2,2,1);
surf(peaks); shading interp
title('Peaks');
ylabel(colorbar, 'Color Scale');

subplot(2,2,2);
plot(rand(10,3));
xlabel('time');
ylabel('money');

subplot(2,2,3);
imshow('peppers.png');

subplot(2,2,4);
surf(membrane(1));
xlabel('x label');
ylabel('y label');
zlabel('z label');

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

tightfig is extremely simple to use. You just call it after creating your plots, and it applies to the current figure. That's one of my favorite things about this entry.

tightfig;

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

Review of the other entries

Before looking into the other entries, I'd like to point out that the use case for tightfig is quite different from that of the others. One is for tightening the figure boundary, and the others are for controlling/tightening the axes boundaries. So it may not be an apples-to-apples comparison. Nonetheless, here I go. Oh, and try not to get confused with all the names. :)

subplot_tight

I find subplot_tight to be the easiest to use, since it has a syntax that is closest to the MATLAB functionsubplot. Not surprisingly, it is a wrapper around subplot, with an added option to specify the spacing between an axes and its neighbors. Because it's a wrapper, you can make use of the vector input syntax for the 3rd parameter (see below). The author also supplies a demo script to recreate his screenshot.

figure;
subplot_tight(2, 2, 1, .1);
subplot_tight(2, 2, 2, .05);
subplot_tight(2, 2, [3 4], .05);

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

tight_subplot and subplot1

tight_subplot and subplot1 are quite similar. They both allow you to lay out a grid of subplots with arbitrary spacings and margins. tight_subplot is compact with just those parameters, i.e. spacing and margin, whilesubplot1 lets you control other axes properties, such as tick labels, label font size, and axes scale. I like that it gives me the ability to have the tick labels only displayed on the outside with subplot1 (see example below).

% tight_subplot

figure;
hA = tight_subplot(3, 2, [.01 .03], [.1 .01], [.01 .01]);

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

% subplot1

figure;
subplot1(3, 2, 'Gap', [.01 .03], 'XTickL', 'Margin', 'YTickL', 'Margin');

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

spaceplots

spaceplots works like tightfig, in that you create your figure first with subplots, then call spaceplots to adjust the spacings and margins. This function, unlike tightfig, will allow you to adjust the spacings between axes, not just the outside margin. The part that I like most is that it will work with irregular-grid subplots (see example below). However, there's a caveat that it only works on axes created using subplot.

figure;
subplot(2, 2, [1 2]); plot(rand(10, 3));
subplot(2, 2, 3); surf(peaks); title('Peaks')
subplot(2, 2, 4); contourf(peaks);

% 0 margin, 0.02 (normalized) spacing
spaceplots([0 0 0 0], [.02 .02]);

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

subplotplus

subplotplus is the king of custom subplots. It comes with a price of somewhat cryptic syntax, but once you understand it (with the help of an example script), it can let you custom layout your subplots in any configuration you like. It even includes the ability to "glue" axes together so that they have a common axis.

cell71={{['-g']};{['-g']};{['-g']};{['-g']};{['-g']};{['-g']};{['-g']}};
cell41={{['-g']};{['-g']};{['-g']};{['-g']}};

figure;
C = {{{{[]},{[]}};cell41},cell71};
[h,labelfontsize] = subplotplus(C);

Matlab中控制图像显示边界、subplot间距等 - 婷子 - 浪漫樱花

Conclusion

So what have I concluded from this review? There are multiple solutions to a problem! They all have unique ways of tackling the problem, and some solve a slightly different problem than others. Overall, I prefer the "post-processing" type functions, tightfig and spaceplots. I tend to do my exploratory plotting in a rough state, and once I have a plot I like, then I start making things look nicer. But of course, with an interactive tool like MATLAB, even the "pre-processing" type functions can be introduced at a later step.

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值