1

I have two numpy arrays representing x and ycoordinates in two dimensions. These (x, y) positions give coordinates of points in a 2D plane. Examples of the arrays x and y are given below.

x = np.array([1.0, 4.0, 3.0, 5.0])
y = np.array([1.5, 4.2, 3.5, 5.2]) 

In the above example, we have coordinates of four two dimensional (x,y) points [i.e.(1.0, 1.5), (4.0, 4.2), (3.0, 3.5), (5.0, 5.2)].

Starting from each of these four two dimensional (x, y) points, I have arrays which represent z coordinates. For this example, I will have four z arrays. Let's consider examples given below.

z1 = np.array([3.1, 3.6, 7.8, 9.2, 11.2, 11.7])
z2 = np.array([4.2, 4.8, 5.9, 10.2])
z3 = np.array([2.1, 2.5, 2.8, 3.5, 4.9])
z4 = np.array([2.0, 4.5, 5.7, 7.2, 7.8, 8.1, 9.2, 15.6])

The different z arrays (z1, z2, z3 and z4) are expected to be of unequal lengths.

I want to have a three dimensional plot of these z arrays (starting from the mentioned (x, y) positions ) using matplotlib. An example of the kind of plot that I am expecting can be found below.  here I tried to start with this:

Axes3D.scatter(xs=x, ys=y, zs=z1, zdir='z)

But, I get the following error:

TypeError: unbound method scatter() must be called with Axes3D instance as first argument (got nothing instead)

How can make the desired three dimensional plot for the simple example (x, y and z arrays) that I have given in this question (I am using python 2.7)?

2 Answers 2

3

What about doing

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

x = np.array([1.0, 4.0, 3.0, 5.0])
y = np.array([1.5, 4.2, 3.5, 5.2])

z1 = np.array([3.1, 3.6, 7.8, 9.2, 11.2, 11.7])
z2 = np.array([4.2, 4.8, 5.9, 10.2])
z3 = np.array([2.1, 2.5, 2.8, 3.5, 4.9])
z4 = np.array([2.0, 4.5, 5.7, 7.2, 7.8, 8.1, 9.2, 15.6])

zs = [
    z1, z2, z3, z4
]

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

for i, x_i in enumerate(x):
    for z_ij in zs[i]:
        ax.scatter(xs=x_i, ys=y[i], zs=z_ij, zdir='z')
plt.show()

enter image description here


Note that we could have done the job without numpy, since we are actually only involving list-contained scalars.

Sign up to request clarification or add additional context in comments.

1 Comment

Any question @SiddTheKid ?
1

Late to the game, but here's another way to do it without the for loops.

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1.0, 4.0, 3.0, 5.0])
y = np.array([1.5, 4.2, 3.5, 5.2])

z1 = np.array([3.1, 3.6, 7.8, 9.2, 11.2, 11.7])
z2 = np.array([4.2, 4.8, 5.9, 10.2])
z3 = np.array([2.1, 2.5, 2.8, 3.5, 4.9])
z4 = np.array([2.0, 4.5, 5.7, 7.2, 7.8, 8.1, 9.2, 15.6])

zs = [z1, z2, z3, z4]
zi_len = [z.shape[0] for z in zs]  # [6, 4, 5, 8]

x_ = np.repeat(x, zi_len)  # [1.0, 1.0, 1.0, 1.0, 1.0,  1.0,  4.0, 4.0, 4.0, 4.0,  3.0, 3.0, 3.0, 3.0, 3.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0]
y_ = np.repeat(y, zi_len)  # [1.5, 1.5, 1.5, 1.5, 1.5,  1.5,  4.2, 4.2, 4.2, 4.2,  3.5, 3.5, 3.5, 3.5, 3.5, 5.2, 5.2, 5.2, 5.2, 5.2, 5.2, 5.2, 5.2]
z_ = np.concatenate(zs)    # [3.1, 3.6, 7.8, 9.2, 11.2, 11.7, 4.2, 4.8, 5.9, 10.2, 2.1, 2.5, 2.8, 3.5, 4.9, 2.0, 4.5, 5.7, 7.2, 7.8, 8.1, 9.2, 15.6]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs=x_, ys=y_, zs=z_, zdir='z', c=z_)
plt.show()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.