0

Just trying to make a 3D plot of a constant (0). So I have

width = 1
dx = 0.1
X = np.arange(-width, width, dx)
Y = np.arange(-width, width, dx)
X, Y = np.meshgrid(X, Y)
Z = []
for i in range(len(X)):
    Z.append(np.zeros(len(X[i])))

But when I try to run Axes3D.plot_wireframe(X,Y,Z) I get plot_wireframe() missing 1 required positional argument: 'Z'. I need help understanding why this is, because Z is a 2D array like it should be, and I can't find many helpful examples with 3D plotting with matplotlib.

2 Answers 2

2

The main point is that you cannot run Axes3D.plot_wireframe(X,Y,Z) by itself. Instead you need to create an instance of Axes3D and call its method [*]. Just like in the 2D case where you wouldn't call matplotlib.axes.Axes.plot(x,y) but ax.plot(x,y) where ax is the instance created e.g. via fig.add_subplot.

An example for the wireframe plot can be found here.

The following code (using the code from the question)

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

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

width = 1
dx = 0.1
X = np.arange(-width, width, dx)
Y = np.arange(-width, width, dx)
X, Y = np.meshgrid(X, Y)
Z = []
for i in range(len(X)):
    Z.append(np.zeros(len(X[i])))

ax.plot_wireframe(X, Y, Z)

plt.show()

produses the following plot

enter image description here


[*] To be precise here; you can call the class method plot_wireframe, but you would then need to supply it with the instance like

Axes3D.plot_wireframe(ax, X, Y, Z)
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, forgot to mention I was using Jupyter. So normally when I create plots I don't normally have to create figures or subplot or all the like. So I guess I assumed the line I had would create the 3D plot.
You always create figures and subplots, independent of where you type in your code.
why is that? jupyter allows you not to, no?
Jupyter is just a way to interprete python code. The result of the code may be presented in a different way, but the python code that produces the result is the same everywhere. Any matplotlib plot will consist of a figure and at least one axes object, and they need to be created somewhere.
So where are they created in jupyter if I don't explicitly have them?
|
1

I found this example online. I've pasted it in your code example and I got the following code + plot:

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

width = 1
dx = 0.1
X = np.arange(-width, width, dx)
Y = np.arange(-width, width, dx)
X, Y = np.meshgrid(X, Y)
Z = []
for i in range(len(X)):
    Z.append(np.zeros(len(X[i])))

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

ax.plot_wireframe(X, Y, Z)

plt.show()

enter image description here

Hope this helps! I ran it with python 3.5, using the spyder IDE.

Cheers, Dave

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.