Making 3D plots

There are some useful matplotlib tool kits and modules that can be used for a variety of special purposes. In this section, we describe a method for producing 3D-plots.

The mplot3d toolkit provides 3D plotting of points, lines, contours, surfaces, and all other basic components as well as 3D rotation and scaling. Making a 3D plot is done by adding the keyword projection='3d' to the axes object as shown in the following example:

from mpl_toolkits.mplot3d import axes3d

fig = figure()
ax = fig.gca(projection='3d')
# plot points in 3D
class1 = 0.6 * random.standard_normal((200,3))
ax.plot(class1[:,0],class1[:,1],class1[:,2],'o')
class2 = 1.2 * random.standard_normal((200,3)) + array([5,4,0])
ax.plot(class2[:,0],class2[:,1],class2[:,2],'o')
class3 = 0.3 * random.standard_normal((200,3)) + array([0,3,2])
ax.plot(class3[:,0],class3[:,1],class3[:,2],'o')

As you can see, you need to import the axes3D type from mplot3d. The resulting plot displays the scattered 3D-data which can be seen in the following figure (Figure 6.14)

Making 3D plots

Figure 6.14: Plotting 3D data using mplot3d toolkit

Plotting surfaces is just as easy. The following example uses the built-in function get_test_data to create a sample data for plotting a surface. Consider the following example of a surface plot with transparency.

X,Y,Z = axes3d.get_test_data(0.05)

fig = figure()
ax = fig.gca(projection='3d')
# surface plot with transparency 0.5 
ax.plot_surface(X,Y,Z,alpha=0.5)

The alpha value sets the transparency. The surface plot is shown in the following figure (Figure 6.15). 

Making 3D plots

Figure 6.15: Example for plotting a surface mesh with three 2D projections.

You can also plot contours in any of the coordinate projections as in the next example.

fig = figure()
ax = fig.gca(projection = '3d')
ax.plot_wireframe(X,Y,Z,rstride = 5,cstride = 5)

# plot contour projection on each axis plane
ax.contour(X,Y,Z, zdir='z',offset = -100)
ax.contour(X,Y,Z, zdir='x',offset = -40)
ax.contour(X,Y,Z, zdir='y',offset = 40)

# set axis limits
ax.set_xlim3d(-40,40)
ax.set_ylim3d(-40,40)
ax.set_zlim3d(-100,100)

# set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

Note the commands for setting the axis limits. The standard matplotlib commands for setting the axis limits are axis([-40, 40, -40, 40]), this works fine for 2D plots. However, axis([-40,40,-40,40,-40,40]) does not work. For 3D plots you need to use the object oriented version of the commands, ax.set_xlim3d(-40,40) and likewise. The same goes for labeling the axis; note the commands for setting the labels. For 2D plots you can do xlabel(’X axis’) and ylabel(’Y axis’) but there is no zlabel command. Instead, in 3D plots you need to use ax.set_xlabel(’X axis’) and likewise, as shown in the preceding example.

The resulting figure from this code is the following

Making 3D plots

There are many options for formatting the appearance of the plots, including color and transparency of surfaces. The mplot3d documentation website, [23], has the details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.186.178