0

With this code:

x=linspace(-3,3,25);
y=x';               
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2)/2);
h=surf(x,y,z);shading interp
%colormap(col4);
set(h,'LineStyle', '-','LineWidth',0.001,'EdgeColor','k');
set(gca, 'YTick',[],'XTick',[],'ZTick',[]);
box on

I can plot a single 3d gaussian: enter image description here

I now want to plot

1) 2 of these side by side within the same axes

2) 4 of these in two rows of two within the same axes

So basically I want a single 3d plot with multiple gaussians on it. Rather than multiple plots of individual Gaussians if that makes sense

...I know this is probably fairly simple, but im stumped. Any help much appreciated.

This was edited to clarify that I want more than one on the same plot, rather than multiple subplots

A crappy mockup of the 2 gaussian version would look like this: enter image description here

2 Answers2

2

The trick is simply to replicate your X and Y matrices using repmat:

x=linspace(-3,3,25);
y=x';               
[X,Y]=meshgrid(x,y);

X = repmat(X, 2, 2);
Y = repmat(Y, 2, 2);

z=exp(-(X.^2+Y.^2)/2);

% note I'm using a different X and Y now in the call to surf()
h=surf(1:size(z,1),1:size(z,2),z);

shading interp
%colormap(col4);
set(h,'LineStyle', '-','LineWidth',0.001,'EdgeColor','k');
set(gca, 'YTick',[],'XTick',[],'ZTick',[]);
box on

For two Gaussians in the same surface, use X = repmat(X, 2, 1), or for more, repmat(X, n, k), etc.

EelkeSpaak
  • 2,757
  • 2
  • 17
  • 37
1

From the matlab documentation, a subplot example which seems to be exactly what you need as suggested by @Ander:

x = 0:0.1:10;
y1 = sin(2*x);
y2 = cos(2*x);

figure
subplot(2,2,1)       % add first plot in 2 x 2 grid
plot(x,y1)           % line plot
title('Subplot 1')

subplot(2,2,2)       % add second plot in 2 x 2 grid
scatter(x,y2)        % scatter plot
title('Subplot 2')

subplot(2,2,3)       % add third plot in 2 x 2 grid
stem(x,y1)           % stem plot
title('Subplot 3')

subplot(2,2,4)       % add fourth plot in 2 x 2 grid
yyaxis left          % plot against left y-axis
plot(x,y1)
yyaxis right         % plot against right y-axis
plot(x,y2)
title('Subplot 4')

Which results in:

enter image description here

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • I think this was what you need, otherwise try plotting multiple lines in 1 graph: `plot(x,y1,x,y2)` – Dennis Jaheruddin Jun 15 '16 at 12:44
  • Thanks for your answer and apologies for being imprecise - Im basically after muliple gaussians on the same plot, rather than multiple plots if that makes sense? – user3519116 Jun 15 '16 at 14:34