0

Question

I am a physicist with data that consists of 4 numpy.ndarrays, with equal lengths (the data I have is huge, with len ~75k). I am looking for a way to sort and plot data from the arrays.

Let's say I have arrays similar to A,B,C,D below:

A = [1,2,3,1,2,3,1,2,3,1,2,3]
B = [1,1,1,2,2,2,1,1,1,2,2]
C = [1,1,1,1,1,1,2,2,2,2,2]
D = [5,6,3,6,3,5,2,4,6,8,7,9]

Let's now say I would like to do a 3D plot of A,D,B' for each value ofC`. How could I automate this?

Chosen solution

With lots of help from RickardSjogren I wrote the following code to plot and save each of the data series for each value of C.

fig = plt.figure()
C_unique = np.unique(C)

for c in zip(C_unique):
    ax = axes(projection='3d')    
    ax.scatter(A[C == c], D[C == c], B[C == c])
    ax.set_xlabel('A')
    ax.set_ylabel('D')
    ax.set_zlabel('B')
    ax.set_title('C = '+str(c))
    savefig(saveDirectory+'/'+str(c))
    clf()
3
  • Can you explain more that what you mean by 3D plot of A,D,B' for each value ofC? Commented Apr 28, 2015 at 10:53
  • Plot A,D,B as a x,y,z 3D projection plot, for each value of C. In the case of the arrays shown there, that would mean plot 2 plots of A,D,B, one where C=1, another where C=2. Commented Apr 28, 2015 at 11:03
  • 1
    So is this question related to those tags? if you are looking for a plotting answer you need to mention it! or maybe you want an array? anyway you need to add the code that you have been tried so far! Commented Apr 28, 2015 at 11:10

1 Answer 1

2

You can do 3D-plots using Matplotlib. From the documentation you simply do:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

And use the plotting methods of the Axes3D-object referenced by ax.

An example using your data for arbitrary number of C-values:

import numpy as np

A = [1,2,3,1,2,3,1,2,3,1,2]
B = [1,1,1,2,2,2,1,1,1,2,2]
C = [1,1,1,1,1,1,2,2,2,2,2]
D = [5,6,3,6,3,5,2,4,6,8,7]
A, B, C, D = map(np.array, [A, B, C, D])

fig = plt.figure()
c_unique = np.unique(C)
fig.set_size_inches(4 * len(c_unique), 4)

# Add axis for each unique C-value.
axes = [fig.add_subplot(1, len(c_unique), i + 1, projection='3d') for i, _ in enumerate(c_unique)]

for c, ax in zip(c_unique, axes):
    # Use boolean indexing of numpy-arrays to plot values for current C.
    ax.scatter(A[C == c], B[C == c], D[C == c])

    ax.set_xlabel('A')
    ax.set_ylabel('B')
    ax.set_zlabel('D')
    ax.set_title('C = {}'.format(c))

fig.tight_layout()

Result:

3D plot

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

1 Comment

Thanks, I've managed to tweak your code for my purposes. This helped a lot.

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.