1、空间散点图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
z = np.random.normal(0, 1, 100)
ax.scatter(x, y, z)
plt.show()
2、空间线型图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
x = np.linspace(-6 * np.pi, 6 * np.pi, 1000)
y = np.sin(x)
z = np.cos(x)
ax.plot(x, y, z)
plt.show()
3、 曲面图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-2, 2, 0.1)
Y = np.arange(-2, 2, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X ** 2 + Y ** 2)
ax.plot_surface(Y, X, Z, cmap=plt.cm.winter)
plt.show()
等高线
ax.contour(X, Y, Z)
注:可以通过offset参数来设置空间等高线投影到哪
plt.contour(X, Y, Z)
关于np.meshgrid()和np.ravel()
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([4,5])
>>> x,y = np.meshgrid(a, b)
>>> x
array([[1, 2, 3],
[1, 2, 3]])
>>> y
array([[4, 4, 4],
[5, 5, 5]])
>>> np.ravel(x)
array([1, 2, 3, 1, 2, 3])
>>> np.ravel(x, order='f')
array([1, 1, 2, 2, 3, 3])
>>> x.flatten(order='f')
array([1, 1, 2, 2, 3, 3])
np.meshgrid(a, b)
函数,按照b的大小扩展a的行数,每一行都是数组a;按照a的大小扩展b的列数,每一列都是数组b。画3D图必备。
np.ravel(x)
把N维的数组压缩为1维。也可以使用x.flatten()
。效果一样
可选参数order='f'
,按列的顺序压缩。
网友评论