Eric Enkele
Eric Enkele

Reputation: 183

How to create a 3d axes object

I'm trying to make a three-dimensional plot, but I can't create the 3D Axes.

When I try, it gives me the an error stating "ValueError: Unknown projection '3d'".

Here's how I've tried to create the Axes object

import matplotlib.pyplot as plt 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

plt.show()

How do I create a 3D Axes object in matplotlib?

Upvotes: 3

Views: 2130

Answers (2)

Paul H
Paul H

Reputation: 68186

In order to create a 3D Axes, you need to import the mplot3d toolkit:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

plt.show()

There are several 3D examples in the gallery: http://matplotlib.org/examples/mplot3d

Upvotes: 7

Brian
Brian

Reputation: 468

From the Matplotlib documentation, "Valid values for projection are: [‘aitoff’, ‘hammer’, ‘lambert’, ‘mollweide’, ‘polar’, ‘rectilinear’]".

You are providing an invalid keyword argument to the add_subplot() method. It looks like you are trying to create a 3D plot in Cartesian coordinates. The projection keyword is not needed to make such a plot.

Upvotes: 0

Related Questions