2

Is there a way to plot a 2D numpy.array with sns.jointplot?

Here is my array:

a = np.array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    1., 0., 0., 0., 0., 0., 1., 1., 2., 1., 2., 1., 1., 1., 0., 0.,
    0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 0., 0., 1., 0., 0.,
    1., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0.,
    0., 0., 0., 0., 0.],
   [0., 0., 2., 1., 2., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1.,
    0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.,
    1., 1., 1., 0., 0.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0.]])

I tried to do df = pd.DataFrame(a), and I get the expected matrix, but then sns.jointplot(df) failed, because I don't know what to use for x and y. It should look like this: https://seaborn.pydata.org/examples/joint_kde.html

any help is highly appreciated. thanks.

Edit: the plot I want is a graphical representation of the values stored in a. The values represent amount of people on locations, and the 2 axis of the data are the x and y coordinates of the floor.

2
  • Could you clarify what you're trying to plot? Is it each sub-array plotted against its index value? Commented Nov 6, 2018 at 0:23
  • I did an edit to the question. does this clarify what I need? Commented Nov 6, 2018 at 0:26

1 Answer 1

2

sns.jointplot does not take a 2D array as an argument. It takes (x,y) coordinates. If you follow the example, that you posted and plot x1 and x2 as a scatterplot you get this:

<code>plt.scatter(x1,x2)</code>

Now if you do the jointplot you get:

<code>g = sns.jointplot(x1, x2, kind="kde", height=7, space=0)</code>

And now you see, that this plots point densities. But the points are from a R->R function. Your 2D array is a R²->R function. If you want it to be reinterpreted as point densities, then you turn it into a R->R function, that looks the same as the old R²->R function when you calculate its density.

Luckily you only have whole numbers, so if you for example want the value 2 at array coordinates [0,24] then you just need two points at the coordinates (0,24) in order to receive the value 2 in density. So all we need to do is find out how many points should be where and then we get the needed two arrays:

#initiate
y1 = np.array([])
y2 = np.array([])

#create locations
for i in range(1,a.max()+1):
    for j in range(i):
        locs = np.where(a==i)
        try:
            y1 = np.append(y1, locs[0])
            y2 = np.append(y2, locs[1])
        except: #this number doesn't exist in the array
            break

for every number n in your array, this will create n points at the right position. You can then call:

g2 = sns.jointplot(y1, y2, kind="kde")

and get your result:

g2 = sns.jointplot(y1, y2, kind="kde")

EDIT Just saw your edit. Well the easiest way to just show your array would be

import matplotlib.pyplot as plt
plt.imshow(a)

which gives:

<code>plt.imshow(a)</code>

EDIT2 after looking a bit more into your problem: It should be way easier and more exact to store the coordinates of the people in the room.

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

3 Comments

Thank you for the great answer. Now I understand the basics of that plot. Funy enough, I ended with R2->R because I also did the heat map. You are right with your second edit, this is actually the original format of the data :-)
Nice, then you don't have to deal with the Shenannigans of creating new arrays just to simulate densities :D
Downvoting for np.append in a loop.

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.