Hi, Sisi is here! I decided to contribute to the journal club blog again. It's not that easy to get rid of me after all... I also decided to start my own blog at http://binarymumbles.blogspot.com/.
Now on to the bussiness.
If you use Matlab to plot some data (esp. time series data, say if your want to plot some behavior over a long period of time), you will
likely encounter the following problem. Sometimes, the ratio of the
figure just looks ugly. This post points out several ways to solve this
problem. If you are in a hurry, just scroll down and read code snippet
4.
try the following code:
% ========== code snippet 1 ==========
y=rand(2000,1); % generate 2000 random numbers
x=1:1:2000; % generate indices
plot(x,y,'color','k'); % plot
% ========== end of snippet ==========
You will get something like this:
This figure will look more pleasing if it is stretched horizontally. To do that, we can add a statement that modify the size of the figure.
% ========== code snippet 2 ==========
y=rand(2000,1);
x=1:1:2000;
fig=figure; % create a figure handle
set(fig,'Position',[0 0 1000 200]); % change figure height to 2000 pixels and width to 200 pixels
plot(x,y,'color','k');
figname='fig1';
print(fig,'-dtiff',figname); % saving figure to tiff format
% ========== end of snippet ==========
This is how the figure look on the screen, x-axis is more stretched out, giving a better view. But if you look at the saved tiff file "fig1.tiff", you will find that the saved figure looks like the earlier figure, the one with a shorter x-axis. So, now the question is, how do we save the figure as it appears on the screen? After some internet search, I found out this easy hack for windows: simply save your figure in bitmap format instead of tiff (or other figure formats). Figures in bitmap format are displayed at screen resolution, therefore, saving as bitmap preserve the ratio.
% ========== code snippet 3 ==========
y=rand(2000,1);
x=1:1:2000;
fig=figure; % create a figure handle
set(fig,'Position',[0 0 1000 200]); % change figure height to 2000 and width to 200
plot(x,y,'color','k');
figname='fig2';
print(fig,'-dbitmap',figname); % saving figure to bitmap format
% ========== end of snippet ==========
However, this solution is suboptimal in several ways:
(1) It only work on windows.
(2) It is not ideal for printing high quality pictures. You can not get a figure that have more pixels than your screen resolution. (Change line 4 in code snipet 3 to set(fig,'Position',[0 0 10000 2000]); and check the height and width of the saved figure).
Is there a better way? Well, absolutely! Otherwise I will not be writing this post :)
% ========== code snippet 4 ==========
y=rand(2000,1);
x=1:1:2000;
fig=figure;
plot(x,y,'color','k');
daspect([500 1 1]); % change aspect ratio of x-y axis (in this example, the length of 500 unit on x axis equals 1 unit on y axis)
figname='fig3';
print(fig,'-dtiff','-r300',figname);
% ========== end of snippet ==========
Now everything is pretty!
No comments:
Post a Comment
Thoughts? Leave a comment!