使用PolyCollection在matplotlib中绘图

4
我正在尝试在matplotlib中绘制三维图。我需要在一个3D图中为四个(或多个)半径绘制频率与振幅分布图。我看了一下matplotlib.collections中可用的PolyCollection命令,并且也浏览了示例,但是我不知道如何使用现有数据得出绘图结果。
我的量的维度为, 频率:4000 x 4, 振幅:4000 x 4, 半径:4
我想绘制这样的图像: enter image description here X轴表示频率,Y轴表示半径,Z轴表示振幅。请问该如何解决这个问题?
1个回答

6

PolyCollection 期望一组顶点,这与您的数据相当匹配。由于您没有提供任何示例数据,因此我将为说明而进行一些假设(我的200维将是您的4000维..尽管如果您有如此多的数据点,我可能会考虑其他绘图方式):

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

# These will be (200, 4), (200, 4), and (4)
freq_data = np.linspace(0,300,200)[:,None] * np.ones(4)[None,:]
amp_data = np.random.rand(200*4).reshape((200,4))
rad_data = np.linspace(0,2,4)

verts = []
for irad in range(len(rad_data)):
    # I'm adding a zero amplitude at the beginning and the end to get a nice
    # flat bottom on the polygons
    xs = np.concatenate([[freq_data[0,irad]], freq_data[:,irad], [freq_data[-1,irad]]])
    ys = np.concatenate([[0],amp_data[:,irad],[0]])
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors = ['r', 'g', 'c', 'y'])
poly.set_alpha(0.7)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# The zdir keyword makes it plot the "z" vertex dimension (radius)
# along the y axis. The zs keyword sets each polygon at the
# correct radius value.
ax.add_collection3d(poly, zs=rad_data, zdir='y')

ax.set_xlim3d(freq_data.min(), freq_data.max())
ax.set_xlabel('Frequency')
ax.set_ylim3d(rad_data.min(), rad_data.max())
ax.set_ylabel('Radius')
ax.set_zlim3d(amp_data.min(), amp_data.max())
ax.set_zlabel('Amplitude')

plt.show()

这大部分内容都来自于你提到的示例,我只是让你清楚了解你特定数据集的位置。这将产生以下图表: PolyCollection示例图


哦,哇!非常感谢。它完美地运行了。另外,您会建议使用哪种其他类型的绘图吗? :) - Karthik Venkatesh
可能实际上只是一组普通的线图 - 如果数据重叠太多,您可以为每个图形偏移零点,或在不同的子图上绘制它们... matplotlib页面上有很多示例可供参考 :) - Ajean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接