简体   繁体   English

Matplotlib 未绘制所有点

[英]Matplotlib not plotting all points

I am trying to plot a 3D-Array in matplotlib, but I only see a linear output.我正在尝试 plot matplotlib 中的 3D 阵列,但我只看到线性 output。 The expected output was a 10x10x10 cube.预期的 output 是 10x10x10 立方体。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

points = np.zeros((10, 10, 10))
for x in range(10):
    for y in range(10):
        for z in range(10):
            points[x][y][z] = z

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:,0],points[:,1],points[:,2])
plt.show()

在此处输入图像描述

OK, you were very, very close.好的,你非常非常接近。 I didn't realize how close until I tried it.直到我尝试过,我才意识到有多接近。 The problem you had was that you made points a 3D array where each entry had a value.您遇到的问题是您在 3D 数组中创建了点,其中每个条目都有一个值。 It needed to be a 2D array, 1000 x 3.它需要是一个 1000 x 3 的二维数组。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

points = []
for x in range(10):
    for y in range(10):
        for z in range(10):
            points.append((x,y,z))

points = np.array(points)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:,0],points[:,1],points[:,2])
plt.show()

You've got a good answer by Tim.蒂姆给了你一个很好的答案。 However, there are alternatives approaches.但是,有替代方法。 For example, there is np.meshgrid() that are often used in your situation to produce and manipulate data.例如,在您的情况下经常使用np.meshgrid()来生成和操作数据。 Here is the code to generate array of data and produce sample plot.这是生成数据数组并生成示例 plot 的代码。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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

n1 = 10  #number of grid rows/columns
xg, yg = np.meshgrid(np.arange(n1),np.arange(n1))

for i in np.arange(n1):
    zg = np.ones(xg.shape) * i
    ax.scatter(xg, yg, zg, s=3, c='k')

lim = n1 + 0.1*n1
ax.set_xlim3d(-0.1*n1, lim)
ax.set_ylim3d(-0.1*n1, lim)
ax.set_zlim3d(-0.1*n1, lim)

# set viewing angle
ax.azim = 120   # z rotation (default=270); 160+112
ax.elev = 35    # x rotation (default=0)
ax.dist = 10    # zoom (define perspective)

plt.show()

立方体2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM