Sunday, February 20, 2011

Inset plots in Matlab

Here's a Matlab code snippet that I've found to be useful. Something I find myself needing to do pretty regularly is to create a plot that has a smaller plot as an inset. (This is common because many journals have pretty strict space limits, so you've got to try and pack as much info as possible into the small set of figures you're allowed...)

X = -5*pi:0.05:5*pi;
Y = sin(X)./X;

figure(1)
clf
plot(X,Y,'b-','LineWidth',1)
hold on
ylabel('sin(x)/x')
xlabel('0 \leq x \leq 5\pi')
title('Halfway to a mexican hat')
axis([0 5*pi -0.3 1.1])
h1 = figure(1);
h2 = get(h1,'CurrentAxes');
% Note: edit these numbers to change position
% and size of inset plot
h3 = axes('pos',[.575 .575 .31 .31]);
plot(X,Y,'b-')
xlabel('-5\pi \leq x \leq 5\pi')
axis([-5*pi 5*pi -0.3 1.1])

When you run this, you get the following extremely sexy plot:
Full disclosure: I copied this code from somewhere else a while ago, but I can't remember where. (Cool story, huh?) Anyway, thought I'd post it here, in case it is useful to someone.

No comments:

Post a Comment