Draw a 3D-surface

example figure

Code

function anim_camera(fname)

% draw a sombrero and turn it around
n = 50;
x = y = linspace (-8, 8, n)';
[xx, yy] = meshgrid (x, y);
r = sqrt (xx .^ 2 + yy .^ 2) + eps;
c = 5 * sin (r) ./ r;

h= surf(xx,yy,c,c);
shading interp

m = moviefile(fname);

for angle=linspace(0,2*pi,50)
  set(gca,'CameraPosition',20*[sin(angle) 0 cos(angle) ]);
  set(gca,'CameraUpVector',[-cos(angle) 0 sin(angle) ]);
  
  drawnow
  sleep(.01)
  m = movieaddframe(m);
end

m = movieclose(m);

Download anim_camera.m

Draw several curves with different styles

example figure

Code

title('Some line propeties');
x = 0:0.1:6;
hold on
plot(x,sin(x),'b');
plot(x,sin(x-.4),'ro');
plot(x,sin(x-.8),'g--');
plot(x,sin(x-1.2),'c+');
hold off

Download test_plot.m

Draw the GNU Octave logo

example figure

Code

title('The GNU Octave logo');
n = 50;
x = y = linspace (-8, 8, n)';
[xx, yy] = meshgrid (x, y);
r = sqrt (xx .^ 2 + yy .^ 2) + eps;
c = 5 * sin (r) ./ r;
h= surf(xx,yy,c,c);
shading interp

Download test_sombrero.m

Draw a 3D surface

example figure

Code

title('Demo of surf, colormap and colorbar');

[x,y] = meshgrid(linspace(-pi,pi,20));
c = cos(x) .* cos(y);

surf(x,y,c,c);
shading faceted
colorbar

Download test_surf.m

Add text to a graphic

example figure

Code

k = linspace(-4,4,100);

omega = sqrt(1+k.^2);

hold on
plot(k,omega,'b');
plot(k,k,'r');
plot(k,-omega,'b');
plot(k,-k,'r');

plot(0,1,'b.')
h1=text(0,1,'inertia-waves');
set(h1,'HorizontalAlignment','center','color',[0 0 1])

h2=text(2.4,2.4,'gravity-waves');
set(h2,'color',[1 0 0])

hold off

Download test_text.m

Draw a line in a 3D space

example figure

Code

title('Demo of plot3');
z = linspace(-4*pi,4*pi,100);

hold on
h1=plot3(cos(z),sin(z),z,'r');
h2=plot3(-cos(z),-sin(z),z,'b');
set([h1 h2],'LineWidth',3);
axis([-4 4 -4 4 -13 13]);
hold off

set(gca,'CameraPosition',[0 40 3]);

Download test_helix.m

Draw a scalar field

example figure

Code

[x,y] = meshgrid(linspace(-pi,pi,30));
z = cos(x).*sin(y);

subplot(1,2,1)
title('Demo of pcolor: shading flat');
pcolor(x,y,z);
shading flat

subplot(1,2,2)
title('shading interp');
pcolor(x,y,z);
shading interp

Download test_pcolor.m

Complex shapes

example figure

Code

load(file_in_loadpath('coastline.octave'));

hold on

% loop over all continents and islands
% lakes have a negative area

index = find(Area>0);

for l=1:length(index); 
  i = index(l);
  j=k(i)+1:k(i+1)-1;  

  patch(ncst(j,1),ncst(j,2),[.4 .8 .5]);
end

Download test_coastline.m

Test EPS and PNG output drivers

example figure

Code

title('Normal distribution');
x = -4:0.1:4;
f = exp(-x.^2/2);
hold on
plot(x,f,'k');
patch([x(21) x(21:61) x(61) x(61)],[0 f(21:61) 0 0],[0.8 1 .8])
patch([x(1) x(1:21) x(21) x(1)],[0 f(1:21) 0 0],'r')
patch([x(61) x(61:end) x(end) x(1)],[0 f(61:end) 0 0],'r')
hold off

print -depsc test_print.eps
print -dpng test_print.png

Download test_print.m

Plot a vector field

example figure

Code

title('Vector field');
[x,y] = meshgrid(1:20);
u = cos(2*pi*y/10);
v = cos(2*pi*x/10);
quiver(x,y,u,v,'b',1);

Download test_quiver.m

Example for subplot

example figure

Code

[x,y] = meshgrid(linspace(-pi,pi,100));
 
subplot(2,2,1);
title('Vibration modes of a rectangular membrane');
z = cos(x/2).*cos(y/2); 
surf(x,y,z,z)

subplot(2,2,2);
z = sin(x).*cos(y/2); 
surf(x,y,z,z)

subplot(2,2,3);
z = sin(y).*cos(x/2); 
surf(x,y,z,z)

subplot(2,2,4);
z = sin(x).*sin(y); 
surf(x,y,z,z)

Download test_subplot.m

Draw a torus

example figure

Code

[phi,theta] = meshgrid(linspace(0,2*pi,100));
x =  (cos(phi) + 3) .* cos(theta);
y =  (cos(phi) + 3) .* sin(theta);
z = sin(phi);
c = sin(3*theta); 
 
surf(x,y,z,c)
shading interp
title('this is not a doughnut');

Download test_torus.m

Filled contours

example figure

Code

n = 50;
x = y = linspace (-8, 8, n)';
[xx, yy] = meshgrid (x, y);
r = sqrt (xx .^ 2 + yy .^ 2) + eps;
c = 5 * sin (r) ./ r;

contourf(x,y,c);

Download test_contourf.m

Scatter plot

example figure

Code

n = 1000;
x = pi * randn(n,1);
y = pi * randn(n,1);

c = cos(x) .* cos(y);

scatter(x,y,4,c);
colorbar

Download test_scatter.m

3D Scatter plot

example figure

Code

[r,phi,theta] = ndgrid(3:6,linspace(0,2*pi,20),linspace(0,pi,10));


x = r .* sin(theta) .* cos(phi);
y = r .* sin(theta) .* sin(phi);
z = r .* cos(theta);

scatter3(x(:),y(:),z(:),3,r(:));

Download test_scatter3.m

Plotting scalar and vector field

example figure

Code

% load sample data

load wfs_data.octave

SST(SST==0) = NaN;

% enable overplotting

hold on

% draw temperature

pcolor(lon,lat,SST); colorbar

% draw velocity

quiver(lon2,lat2,u,v,'k')

% set title

title('Surface temperature and velocity')

% draw coastline

plot_coastline('gshhs_l.octave',[-90.519  -80.397],[24.271   30.816])

hold off

% change size of figure (in pixels) to get a correct 
% aspect ratio for print

set(gcf,'PaperPosition',[0 0 600 350]);



Download test_wfs.m

Draw a scalar field on a sphere

example figure

Code

function anim_globe(fname)

% load example data

load levitus_data.octave

% minimum temperatur is -3
% put continents to -4

temp(isnan(temp)) = -4; 

% add a the colormap entry for
% continents in gray

map = [0.7 0.7 0.7; colormap];
colormap(map)

% close the globe

lon = [lon; 365.5];
temp = [temp; temp(1,:)];

% convert to sperical coordinates

phi = lon*pi/180;
theta = (90 - lat)*pi/180;

[phi,theta] = ndgrid(phi,theta);

% cartesian coordinates

x = sin(theta) .* cos(phi);
y = sin(theta) .* sin(phi);
z = cos(theta);

% plot the globe with color 
% representing sea surface temperature

surf(x,y,z,temp)
shading flat

title('Sea Surface Temperature');

% don't show axes
axis off

% rotate the camera

m = moviefile(fname);

for angle=0:0.1:2*pi
  set(gca,'CameraPosition',20*[cos(angle) -sin(angle) .5]);
  drawnow
  m = movieaddframe(m);  
end

m = movieclose(m);

Download anim_globe.m

Image as red, green, blue values

example figure

Code

% check availability of imread
if which('imread')
  c = imread(file_in_loadpath('yapso_test_image.jpg'));
  c = c(end:-1:1,:,:);

  image(c);
else
  disp('This examples requires the image package (from octave-forge)');
end

Download test_image.m

Hosted by SourceForge.net Logo