使用mplot3d Polycollection(或其他mplot3d库)创建三维子地块的网格
下面是一个简单的mplot3d示例(您可以在这里看到:https://matplotlib.org/2.0.2/examples/mplot3d/polys3d_demo.html):
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
xs = np.arange(0, 10, 0.4)
verts = []
zs = [0.0, 1.0, 2.0, 3.0]
for z in zs:
ys = np.random.rand(len(xs))
ys[0], ys[-1] = 0, 0
verts.append(list(zip(xs, ys)))
poly = PolyCollection(verts,lw=1, closed=False)
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')
ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 2)
plt.show()
现在我有大量的切片,我想在几个子图中绘制它们。我已经尝试了一些代码(例如https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple-subplots.html),包括:
verts = []
zs = np.arange(1000)
for z in zs:
df = rfft(tiles[z])
ys = 2.0/100 * np.abs(df[0:100//2])
ys[0], ys[-1] = 0, 0
verts.append(list(zip(xs, ys)))
fig = plt.figure(figsize=(15,8))
for i in range(40):
plt.subplot(20, 2, i)
ax = fig.gca(projection='3d')
poly = PolyCollection(verts[i*25:(i+1)*25],lw=1)
ax.add_collection3d(poly, zs=np.arange(25), zdir='y')
但结果,我得到的只是一个正常的垂直连续的数字。完全没有子图形。 那么如何将子绘图与三维绘图结合起来(这里使用的是polycollection) 谢谢
我将使用
plt.subplots()
传递参数subplot_kw=dict(projection='3d')
来创建轴,以直接将所有子图创建为Axes3D对象。然后只需在轴上迭代,并根据需要填充它们。你知道吗相关问题 更多 >
编程相关推荐