0

I have a numpy array like this:

[[[0,0,0], [1,0,0], ..., [1919,0,0]],
[[0,1,0], [1,1,0], ..., [1919,1,0]],
...,
[[0,1019,0], [1,1019,0], ..., [1919,1019,0]]]

To create I use function (thanks to @Divakar and @unutbu for helping in other question):

def indices_zero_grid(m,n):
     I,J = np.ogrid[:m,:n]
     out = np.zeros((m,n,3), dtype=int)
     out[...,0] = I
     out[...,1] = J
     return out

I can access this array by command:

>>> out = indices_zero_grid(3,2)
>>> out
array([[[0, 0, 0],
        [0, 1, 0]],

       [[1, 0, 0],
        [1, 1, 0]],

       [[2, 0, 0],
        [2, 1, 0]]])
>>> out[1,1]
array([1, 1,  0])

Now I wanted to plot 2d histogram where (x,y) (out[(x,y]) is the coordinates and the third value is number of occurrences. I've tried using normal matplotlib plot, but I have so many values for each coordinates (I need 1920x1080) that program needs too much memory.

6
  • How many coordinates do you have? Commented Oct 4, 2017 at 7:49
  • I have 1920 x 1080 Commented Oct 4, 2017 at 7:59
  • That is practically a screen resolution. If your screen has that resolution each bar will be a pixel. Are you sure that this is what you want? Why not make an image (imshow) plot or a surface? Commented Oct 4, 2017 at 8:05
  • @armatita Yes, this is screen resolution. But I need histogram because I wanted to create some kind of map of activity. Third element of each coordinates is a repetition level of each pixel in my specific application. Commented Oct 4, 2017 at 8:15
  • 2
    Consider adding a minimal case and the expected output. To create the minimal one, you can use something like indices_zero_grid(3,4) from your previous question's answer. Commented Oct 4, 2017 at 8:22

1 Answer 1

1

If I understand correctly, you want an image of size 1920x1080 which colors the pixel at coordinate (x, y) according to the value of out[x, y].

In that case, you could use

import numpy as np
import matplotlib.pyplot as plt

def indices_zero_grid(m,n):
     I,J = np.ogrid[:m,:n]
     out = np.zeros((m,n,3), dtype=int)
     out[...,0] = I
     out[...,1] = J
     return out

h, w = 1920, 1080
out = indices_zero_grid(h, w)
out[..., 2] = np.random.randint(256, size=(h, w))
plt.imshow(out[..., 2])
plt.show()

which yields

enter image description here

Notice that the other two "columns", out[..., 0] and out[..., 1] are not used. This suggests that indices_zero_grid is not really needed here.

plt.imshow can accept an array of shape (1920, 1080). This array has a scalar value at each location in the array. The structure of the array tells imshow where to color each cell. Unlike a scatter plot, you don't need to generate the coordinates yourself.

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

1 Comment

You're right! This solution works perfectly. I don't really need the other two "columns" and it works faster than previous. I didn't think about it. Thanks You very much!

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.