同一p中的两个不同的图

2024-04-23 22:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在同一个图中绘制线框和散点图。我要做的是:

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

fig = plt.figure()

ax1 = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

ax2 = fig.add_subplot(111, projection='3d')
xs = np.array( [ 1, 0 ,2 ])
ys = np.array( [ 1, 0, 2 ])
zs = np.array( [ 1, 2, 3 ])
ax2.scatter(xs, ys, zs)

plt.show()

这个脚本只是给出了散点图。注释任何块,就得到未注释的绘图。但他们不会在一起搞同一个阴谋。在


Tags: importaddasnpfigpltarrayxs
1条回答
网友
1楼 · 发布于 2024-04-23 22:27:44

当您再次add_subplot(111)时,您将重写上一个子批。只是不要这样,在同一个轴上画两次:

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')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

xs = np.array( [ 1, 0 ,2 ])
ys = np.array( [ 1, 0, 2 ])
zs = np.array( [ 1, 2, 3 ])
ax.scatter(xs, ys, zs)

plt.show()

相关问题 更多 >