I want to make a 3D Graph with Matplotlib. The graph window appears, but no data is shown. What am I doing wrong?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [0, 10, 20, 40, 100]
y = [1, 4, 8, 60, 200]
z = [4, 5, 6, 7, 8]
ax.plot_surface(x, y, z)
plt.show()
asked Mar 13, 2013 at 17:41
ustroetz
6,38218 gold badges53 silver badges77 bronze badges
2 Answers 2
plot_surface expects 2D inputs (doc). It is not plotting anything because you did not give it a valid surface to draw.
See this example.
answered Mar 13, 2013 at 18:13
tacaswell
88.2k23 gold badges222 silver badges203 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
X, Y and Z needs to be 2D-arrays :
Surface plots Axes3D.plot_surface(X, Y, Z, *args, **kwargs) Create a surface plot.
Argument Description
X, Y, Z Data values as 2D arrays
However I do not understand the logic behind it : check this SO post for more info.
answered Mar 13, 2013 at 18:12
lucasg
11k4 gold badges38 silver badges58 bronze badges
1 Comment
tacaswell
the logic is that
plot_surface and the underlying functions assume that the connectivity of the points in the surface are given by the position in the matrix. This is X[i,j] has as NN X[i + 1, j], X[i, j + 1], X[i, j - 1], and X[i - 1, j]lang-py