Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab fill contour3 plot like contourf plot

Tags:

plot

matlab

I'd like to fill a 3D contour plot (contour3(X,Y,Z)) like the 2D contour fill plot (contourf(X,Y,Z)). But I can't figure out how this can be achieved. The combination of contour3 and surf is not very satisfying, since there are tiles.

[X,Y,Z] = peaks(32);

figure
contourf(X,Y,Z,15);

figure
contour3(X,Y,Z,15,'k');
hold on;
surf(X,Y,Z, 'Edgecolor', 'none');

contourf(X,Y,Z,15); enter image description here

contour3(X,Y,Z,15,'k'); hold on; surf(X,Y,Z, 'Edgecolor', 'none'); enter image description here

like image 376
konze Avatar asked Feb 26 '26 12:02

konze


1 Answers

the colour on a basic surface plot is function of the Z data. they will either be faceted or interpolated but the contour3 function will not modify the colouring of the surf object. The contour3 function only draws the isolines.

If you want your surface to be coloured in a "blocky" way like a flat colour plot, you have to make the colormap "blocky" as well:
In your example you use 15 isolines, so you have to create a colormap with 15+1 colour so each colour block of the colormap match an isoline.

nContour = 15 ;
figure ; [X,Y,Z] = peaks(32);
surf(X,Y,Z, 'Edgecolor', 'none');
shading interp
colormap( parula(nContour+1) ) %// assign a colormap with only 15+1 colors

Will get you the image on the left of the screenshot below. Now add your isolines on top if you want:

hold on;
[C,h] = contour3(X,Y,Z,nContour,'k');

and you get the plot on the right. You can do both these things in no particular order, just make sure the colormap of the surface is adequate for the number of isolines you want.

contour3ex

like image 125
Hoki Avatar answered Feb 28 '26 14:02

Hoki