Showing posts with label plots. Show all posts
Showing posts with label plots. Show all posts

Friday, March 27, 2015

Flipping the Y axes in Matlab

In most computer graphics APIs/toolkits, the default Y direction is from top to bottom, i.e. the Y origin (y = 0) is the top and increases as it goes downward. However in most scientific plots, the Y direction is the opposite. The positive Y direction is upwards.
Because of this discrepancy, there is always confusion when drawing images, especially in Matlab (e.g. imagesc). While one possibility would be to flip the data array via flipud, my preferred method is to use the 'ydir' option.

set(gca, 'ydir', 'normal');
This leaves the data as is, no flipping data, and only changes the the plot options, which separates data from presentation. (Model/View)
An interesting point is that the default value for ydir is 'reverse' which is the default direction of most graphics packages.

Tuesday, January 28, 2014

Linking Axes

When using multiple plots, especially subplots, it's possible to link the axes using the (you guessed it) linkaxes function. This function will link the axes so that the plots will move and zoom together, making it easier to compare the plots.

t = (0:(length(rc)-1))/fs;
x = sin(2*pi*1000*t);
y = sin(2*pi*1000*t + pi/4);


subplot(211); plot(t, x); 
ax(1) = gca; 
subplot(212); plot(t, y); 
ax(2) = gca; 
linkaxes(ax, 'xy'); 

Monday, January 6, 2014

Docking Matlab Figures


To dock the figure window in Matlab, create a file named startup.m in Matlab's default directory and add the following line.
set(0,'DefaultFigureWindowStyle','docked')

In case you don't know where the default directory is, open Matlab and enter the command pwd in the command window.

To revert the docking behavior, either change the line in startup.m to
set(0,'DefaultFigureWindowStyle','normal')

or delete startup.m.