【发布时间】:2021-01-03 17:27:47
【问题描述】:
我试图在 matplotlib 中绘制网格的散点图。这是我的代码:
import math
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# minvalues = np.min(vertexarray, axis=0) # vertex array of a given polygon
# maxvalues = np.max(vertexarray, axis=0)
minvalues = [-4] * 3 # magic numbers for reproducible example
maxvalues = [4] * 3
detail = 10
meshgriddim = np.linspace(minvalues, maxvalues, num=detail)
sortedmgd = [[i[j] for i in meshgriddim] for j in range(3)]
pointfieldx, pointfieldy, pointfieldz = np.meshgrid(sortedmgd[0], sortedmgd[1], sortedmgd[2])
sortedpf = [(a2, b2, c2,) for a, b, c in zip(pointfieldx, pointfieldy, pointfieldz) for a1, b1, c1 in zip(a, b, c) for a2, b2, c2 in zip(a1, b1, c1)]
sortpfx, sortpfy, sortpfz = zip(*sortedpf)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim3d(minvalues[0], maxvalues[0])
ax.set_ylim3d(minvalues[1], maxvalues[1])
ax.set_zlim3d(minvalues[2], maxvalues[2])
plt.scatter(sortpfx, sortpfy, sortpfz)
plt.show()
这应该会产生一个均匀分布的点立方体。但是,它只在一个平面上生成点:
matplotlib 上的散点图示例测试工作得很好。我尝试了几件事,但没有任何效果。我相信这是一个 matplotlib 问题,因为在打印时,sortpfz 表明存在许多不同的 z 层。
【问题讨论】:
-
@TrentonMcKinney 完成。
标签: python python-3.x numpy matplotlib