0

I am trying to plot a 3D-Array in matplotlib, but I only see a linear output. The expected output was a 10x10x10 cube.

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

enter image description here

3
  • Have you looked at your matrix, just by printing it? You have 10 rows on 10 planes that are all identical. So, points[:,0] == points[:,1] == points[:,2]. In each case, you are telling it to plot the points [0,0,0], [1,1,1], [2,2,2], [3,3,3], etc, up to [9,9,9]. That's what it plotted. Commented Mar 14, 2021 at 1:30
  • Well wouldn't it be [0, 0, 0], [0, 0, 1], [0, 0, 2] etc. When I print x, y, z in the 3rd for loop that's what I see Commented Mar 14, 2021 at 1:39
  • Did you print points[:,0] and points[:,1] and points[:,2]? Those are the planes you are passing in as the x points, the y points, and the z points, and those planes are all identical. It's going to plot the points by picking one from each in corresponding positions. Since the planes are identical, it will plot [n,n,n] for n between 0 and 9. If you were trying to plot 1000 different points in a 10x10x10 grid, I'll post an answer that does that. Commented Mar 14, 2021 at 1:58

2 Answers 2

2

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. It needed to be a 2D array, 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()
Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, I attempted this but was missing converting it to an np array. Thanks!
1

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. Here is the code to generate array of data and produce sample 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()

cube2

1 Comment

I was reading about meshgrid, great to see it in practice. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.