首页 > 代码库 > Generate contour plot in GNU Octave

Generate contour plot in GNU Octave

<style></style>

Step 1: generating the grid points for the 2D contour plot:

[xx, yy] = meshgrid(x_start:dx:x_end, y_start:dy:y_end);

Step 2: Calculate the data values at those grid points: where x, y, z are three arrays holding all the source data.

zz = griddata(x, y, z, xx, yy, "linear");

Step 3: Plot using surfc: disable the edgecolor, otherwise, it will be ugly.

surfc(xx, yy, zz, "edgecolor", "none");

Step 4: Create the color bar.

cb = colorbar("EastOutside", "fontsize", fontsize, "linewidth", 0);

Step 5: Set the view angle of the contour plot. Here, it is set to top view, i.e. a 2D colorful plot will be obtained.

view(2);

Step 6: Set the xlim and ylim to remove unnecessary blank at the boundary.

xlim([x_start, x_end]);

ylim([y_start, y_end]);

Now, let‘s make a contour plot:

h=1;

figure(h);

clf(h);

dx=1e-2;

dy=1e-2;

x_start=0;

x_end=10;

y_start=0;

y_end=10;

NX=100;

NY=100;

x=zeros(NX,NY);

y=zeros(NX,NY);

z=zeros(1,NX*NY);

for m=1:NX

x(m,:)=linspace(x_start,x_end,NY);

endfor

for m=1:NY

y(:,m)=linspace(y_start,y_end,NX);

endfor

x=reshape(x,1,NX*NY);

y=reshape(y,1,NX*NY);

z=sin(x).*sin(y);

[xx,yy]=meshgrid(x_start:dx:x_end, y_start:dy:y_end);

zz=griddata(x, y, z, xx, yy, "linear");

surfc(xx, yy, zz, "edgecolor", "none");

cb = colorbar("EastOutside", "fontsize", 12, "linewidth", 0);

xlim([x_start,x_end]);

ylim([y_start,y_end]);

print(h, "surfc.eps", "-depsc2", "-F:12");

system("eps2png surfc.eps");

view(2);

print(h, "surfc_2d.eps", "-depsc2", "-F:12");

system("eps2png surfc_2d.eps");

The generated figure looks like below: