0

I've run into a bit of a hiccup trying to plot some data in the way I want it - any advice would be greatly appreciated.

left and right are vectors of a few hundred thousand in length, obtained elsewhere. The code below plots left, twice - the second plot lies on top of the first, roughly towards one corner.

ax1 = axes;
plot(ax1, left, 'b');
set(ax1, 'xlim', [7.075*10^4 7.5*10^4]);
set(ax1, 'ylim', [-0.02 0.02]);

ax2 = axes('Position', get(ax1,'Position'), 'XAxisLocation', 'top', 'YAxisLocation', 'right', 'Color', 'none', 'XColor', 'k', 'YColor', 'k', 'NextPlot', 'add');
plot(ax2, left, 'b');
set(ax2, 'Units', 'normalized', 'Position', [0.6 0.60 0.25 0.25]);

What I'd like to do is have the same kind of thing for right, and then display each pair of plots as a subplot, with the two subplots side by side. I've tried adapting the way I'm doing it above to use subplot, but I'm obviously doing something wrong since I keep on nuking the contents of each subplot and ending up with two empty subplots.

Also, is it possible to prevent the smaller inset plot from having a transparent background?

Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
  • 2
    if you use `subplot`, than it sets the different axes positions for you. Therefor if you later change the axes position (with the `set` command), you can mess the subplots. It might help if you post the code that doesn't do what you want. – Itamar Katz Jul 11 '11 at 06:27
  • @IskarJarak: related question: http://stackoverflow.com/questions/1744667/plot-overlay-matlab – Amro Jul 11 '11 at 12:27
  • @Itamar Katz: +1 for a comment which helped me realise what was going wrong. – Iskar Jarak Jul 11 '11 at 23:54

2 Answers2

3

Consider the following example:

%# sample data
x = 1:100;
left = randn(100,1);
right = cumsum(rand(100,1)-0.5);

%# build axes positions
hBig = [subplot(121) subplot(122)];         %# create subplots
posBig = get(hBig, 'Position');             %# record their positions
delete(hBig)                                %# delete them
posSmall{1} = [0.275 0.63 0.16 0.24];
posSmall{2} = [0.717 0.63 0.16 0.24];

%# create axes (big/small)
hAxB(1) = axes('Position',posBig{1});
hAxB(2) = axes('Position',posBig{2});
hAxS(1) = axes('Position',posSmall{1});
hAxS(2) = axes('Position',posSmall{2});

%# plot
plot(hAxB(1), x, left, 'b');
plot(hAxB(2), x, right, 'b');
plot(hAxS(1), x, left, 'r');
plot(hAxS(2), x, right, 'r');

%# set axes properties
set(hAxB, 'XLim',[1 100], 'YLim',[-10 10]);
set(hAxS , 'Color','none', 'XAxisLocation','top', 'YAxisLocation','right');

screenshot

If you want the background color of the smaller axes to be opaque, just set their colors to white:

set(hAxS , 'Color','w')
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Thanks - this does what I want it. Also, I was going to make the side-by-side plots manually after Itamar Katz's comment, but it would never have occurred to me to make subplots, record their positions, and then delete them. – Iskar Jarak Jul 11 '11 at 23:55
1

To change the background, use (for red background)

set(ax2,'color',[1 0 0])

Regarding the subplot, if you post the code that doesn't work it will help.

Itamar Katz
  • 9,544
  • 5
  • 42
  • 74