具有3D多边形集合的子图网格 [英] Grids of Subplots with 3D Polycollection

查看:102
本文介绍了具有3D多边形集合的子图网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mplot3d Polycollection(或另一个mplot3d库)创建3D子图的网格

Create a grid of 3D Subplots using mplot3d Polycollection (or another mplot3d library)

这是一个简单的mplot3d示例(您可以在此处看到: https://matplotlib.org/2.0.2/examples/mplot3d/polys3d_demo.html ):

Here is a simple mplot3d example (you can see here: 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 ),包括类似的内容:

Now I have a large number of slices and I want to plot them within several subfigures. I already tried some codes (see for example https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple-subplots.html), including something like that :

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')

但是,结果是,除了正常的垂直连续数字之外,我什么也没有.完全没有子图. 那么如何将子绘图与3D绘图结合在一起(此处使用polycollection) 谢谢

But as a result, I get nothing but a normal vertical succession of figures. No subfigure at all. So how to combine subplotting with 3D plotting (here using polycollection) Thanks

推荐答案

我将使用plt.subplots()通过传递参数subplot_kw=dict(projection='3d')来创建轴,以直接将所有子图创建为Axes3D对象.然后,只需在轴上迭代并按您认为合适的方式填充它们即可.

I would create the axes using plt.subplots() passing the argument subplot_kw=dict(projection='3d') to directly create all the subplots as Axes3D objects. Then it's just a matter of iterating over the axes and populating them as you see fit.

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)))


fig, axs = plt.subplots(2,2,figsize=(10,10), subplot_kw=dict(projection='3d'))
for i,ax in enumerate(axs.flat):
    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)
    ax.view_init(elev=10.+i*10, azim=25+i*25)

这篇关于具有3D多边形集合的子图网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆