0

I am currently trying to plot a 3D scatter plot by using a 3D array. What I found online about plotting 3D scatter plot looks something like ax.scatter3D(x, y, z) where x, y , z are all 1D array. However, in my case, I am generating an array of shapes(3, 3, 3) by using numpy's histogramdd.

In [61]: h, edges = histogramdd(array([[1,2,4],[4,2,8],[3,2,1],[2,1,2],[2,1,3],[2,1,1],[2,1,4]]),bins=3)
In [64]: h
Out[64]:
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  1.,  0.]],

       [[ 3.,  1.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 1.,  0.,  1.]]])

My question is, how can I unpack this (3, 3, 3) into 1-dimensional arrays that correspond to the axes so that I can plot 3d scatter plot?

5
  • have you tried looking into reshape()? Rather than a 1D array why not make it a nx3 dimention array like so : h.reshape((n,3)). Commented Dec 31, 2020 at 10:29
  • Why I wanted to plot 3D scatter plot from the histogram is because I wanted to see the distribution of the histogram. Does the distribution retain if I reshape it? Commented Dec 31, 2020 at 10:53
  • I think I'm missing the main idea here, tell me where I'm wrong: you have a list of many points in the form [x,y,z], in the example you have [1,2,4], [4,2,8]... and so on. Then you build a 3D histogram and you want to plot it. I think the problem is that you require 4 dimensions to plot that (or 3 dimensions + colors). Am I wrong? Commented Dec 31, 2020 at 11:53
  • I actually have total 4 of the 3d array [1,2,4], [4,2,8]... Commented Dec 31, 2020 at 12:00
  • I still don't understand, you have points in a 3D space, you are slicing this space in 3x3x3 regions (you basically divide the 3D space in 27 little cubes). When you build the histogram you are counting the number of points inside every little cube. As you can see in your result most of the cubes contain zero points inside, while some have 1 or 3 points. In a 3D scatter plot you could plot the position of the cubes as a point in a 3D space, but then you need to change something else to encode the information about the number of data points contained in the cube. Am I correct? Commented Dec 31, 2020 at 12:09

1 Answer 1

1

I would say you need 4 dimensions to plot the histogram you have created. One idea could be using a 3D scatter plot changing the size of the marker to encode the information about the number of data points contained in each of the 3D bins.

Here's what I would do.

  1. generate the 3D histogram
import numpy as np

a = np.array([[1,2,4],[4,2,8],[3,2,1],[2,1,2],[2,1,3],[2,1,1],[2,1,4]])
h, edges = np.histogramdd(a, bins=3)
  1. Create the 3D coordinates of your bins position
ex, ey, ez = edges
x, y, z = np.meshgrid(np.linspace(ex[0], ex[-1], 3),
                      np.linspace(ey[0], ey[-1], 3),
                      np.linspace(ez[0], ez[-1], 3))
  1. Use a 3D scatter to plot your bin and change the size of the markers to encode the number of points contained in each plotted bin:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

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

ax.scatter(x.flatten(), y.flatten(), z.flatten(), s=h.flatten()*50)

Here's the plot result:

3D scatter plot

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

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.