如何在Python中绘制数据立方体
我在想,是否有办法在Python中绘制一个数据立方体。我的意思是,我每个点都有三个坐标。
x=part.points[:,0]
y=part.points[:,1]
z=part.points[:,2]
而且对于每个点,我还有一个标量场t(x,y,z)。
我想绘制一个三维数据立方体,显示每个点的位置,并且每个点的颜色与该点的标量场t成正比。
我尝试过使用histogramdd,但没有成功。
2 个回答
10
你可以使用 matplotlib 这个库。这里有一个可以运行的示例(而且它是动态的!):
import random
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
mypoints = []
for _ in range(100):
mypoints.append([random.random(), #x
random.random(), #y
random.random(), #z
random.randint(10,100)]) #scalar
data = zip(*mypoints) # use list(zip(*mypoints)) with py3k
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data[0], data[1], data[2], c=data[3])
pyplot.show()
你可能需要调整你的数值和对应颜色之间的关系。
Matplotlib 看起来很不错,但当你有很多点的时候,绘制和移动这些三维图形可能会比较慢。在这种情况下,我通常会使用 Gnuplot,并通过 gnuplot.py 来控制它。Gnuplot 也可以直接作为一个子进程使用,具体可以参考 这里 和 这里。