Quantcast
Channel: solem's vision blog
Viewing all articles
Browse latest Browse all 31

3D plotting with Matplotlib

$
0
0
Earlier, Matplotlib (and Python) lacked proper support for 3D plotting. This is no longer the case as 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 plot 3D is done by adding the projection=‘3d’ keyword to the axes object like this:

from pylab import *
from numpy import *
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')



3D scatter plots of three distributions.

Plotting surfaces is just as easy. The following example uses get_test_data() to create sample data.

# generate 3D sample data
X,Y,Z = axes3d.get_test_data(0.05)

fig = figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z,alpha=0.5)



Surface plot with transparency.

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

Viewing all articles
Browse latest Browse all 31

Trending Articles