Note

Go to the endto download the full example code.

Intersecting planes#

This examples demonstrates drawing intersecting planes in 3D. It is a generalizationof2D images in 3D.

Drawing intersecting planes inmplot3d is complicated, becausemplot3d is not areal 3D renderer, but only projects the Artists into 3D and draws them in the rightorder. This does not work correctly if Artists overlap each other mutually. In thisexample, we lift the problem of mutual overlap by segmenting the planes at theirintersections, making four parts out of each plane.

This examples only works correctly for planes that cut each other in haves. Thislimitation is intentional to keep the code more readable. Cutting at arbitrarypositions would of course be possible but makes the code even more complex.Thus, this example is more a demonstration of the concept how to work aroundlimitations of the 3D visualization, it's not a refined solution for drawingarbitrary intersecting planes, which you can copy-and-paste as is.

importmatplotlib.pyplotaspltimportnumpyasnpdefplot_quadrants(ax,array,fixed_coord,cmap):"""For a given 3d *array* plot a plane with *fixed_coord*, using four quadrants."""nx,ny,nz=array.shapeindex={'x':(nx//2,slice(None),slice(None)),'y':(slice(None),ny//2,slice(None)),'z':(slice(None),slice(None),nz//2),}[fixed_coord]plane_data=array[index]n0,n1=plane_data.shapequadrants=[plane_data[:n0//2,:n1//2],plane_data[:n0//2,n1//2:],plane_data[n0//2:,:n1//2],plane_data[n0//2:,n1//2:]]min_val=array.min()max_val=array.max()cmap=plt.get_cmap(cmap)fori,quadrantinenumerate(quadrants):facecolors=cmap((quadrant-min_val)/(max_val-min_val))iffixed_coord=='x':Y,Z=np.mgrid[0:ny//2,0:nz//2]X=nx//2*np.ones_like(Y)Y_offset=(i//2)*ny//2Z_offset=(i%2)*nz//2ax.plot_surface(X,Y+Y_offset,Z+Z_offset,rstride=1,cstride=1,facecolors=facecolors,shade=False)eliffixed_coord=='y':X,Z=np.mgrid[0:nx//2,0:nz//2]Y=ny//2*np.ones_like(X)X_offset=(i//2)*nx//2Z_offset=(i%2)*nz//2ax.plot_surface(X+X_offset,Y,Z+Z_offset,rstride=1,cstride=1,facecolors=facecolors,shade=False)eliffixed_coord=='z':X,Y=np.mgrid[0:nx//2,0:ny//2]Z=nz//2*np.ones_like(X)X_offset=(i//2)*nx//2Y_offset=(i%2)*ny//2ax.plot_surface(X+X_offset,Y+Y_offset,Z,rstride=1,cstride=1,facecolors=facecolors,shade=False)deffigure_3D_array_slices(array,cmap=None):"""Plot a 3d array using three intersecting centered planes."""fig=plt.figure()ax=fig.add_subplot(projection='3d')ax.set_box_aspect(array.shape)plot_quadrants(ax,array,'x',cmap=cmap)plot_quadrants(ax,array,'y',cmap=cmap)plot_quadrants(ax,array,'z',cmap=cmap)returnfig,axnx,ny,nz=70,100,50r_square=(np.mgrid[-1:1:1j*nx,-1:1:1j*ny,-1:1:1j*nz]**2).sum(0)figure_3D_array_slices(r_square,cmap='viridis_r')plt.show()
intersecting planes

Tags:plot-type: 3Dlevel: advanced

Total running time of the script: (0 minutes 1.568 seconds)

Gallery generated by Sphinx-Gallery