Consider this MWE:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
n = 15
m = 12
x = np.linspace(-5, 5, n)
y = np.linspace(-5, 5, m)
Z = np.zeros((m, n))
for i in xrange(m):
for j in xrange(n):
Z[i, j] = x[j]**2 + y[i]**2
### Plot surface ###
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y = np.meshgrid(x, y)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Z')
plt.show()
Note in particular that the dimensions n
and m
are not equal. The resulting plot has some weird lines hanging down, as well as strange coloring:
What's going on here, and how can I prevent this?
Unlike 2D, 3D plots in matplotlib have a lot of shortcomings. Let me quote one of the answers in matplotlib FAQ:
For your particular problem (and notice that I don't think this has anything to do with different sizes in each direction) I would advise you to increase your surface shape (even if artificially) and play around with the number of strides until you obtain something that is satisfactory:
,which results in this:
The example above give rstrides and cstrides a value of 10. Should you increase it too much (let's say 80) and the problem becomes obvious:
Other option is for you to follow the recommendation of matplotlib FAQ itself and check Mayavi. Notice, however, that mayavi still does not support Python 3. Personally, if you need something quick to work with, I would recommend PyQtGraph.