0

I have an array looking like this:

    array([[ 912.1,  821.5],
           [ 911.9,  821.5],
           [ 911.9,  821.5],
           ..., 
           [ 654.6,  552.8],
           [ 655. ,  553.3],
           [ 655.4,  553.7]])

So there are like 250,000 coordinates organized like that. Each contains one x-value and one y-value. I want to create a 2D histogram using this data. I've been looking at tutorials, but they all seem to show how to create 2D histograms from random data and not a numpy matrix like that. So in the end it should be a heatmap where red indicates more counts than blue for example.

Has anyone any ideas or hints to solve this kind of problem?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68

1 Answers1

1

You can simply split the array into individual vectors for x and y:

x = arr[:,0]
y = arr[:,1]

Then, you can create a 2D histogram in the standard way using hist2d:

plt.hist2d(x, y)
plt.colorbar()
plt.show()

Or, in one line:

plt.hist2d(arr[:,0], arr[:,1])

For further flexibility, e.g. changing the colorscale please read the documentation for numpy.histogram2d.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Thank you for the quick answer! – Antonia Botticelli Dec 05 '15 at 12:52
  • I see I have a second problem. Some coordinates in this array are: ... [nan, nan], [nan, nan], ... so there are no values. I want to leave them out or skip them. If I don't do so I get this error: ValueError: cannot convert float NaN to integer Is there a possibility to do so? – Antonia Botticelli Dec 05 '15 at 13:00
  • Please post a new question including an example. Or, even better, do a little research first. See e.g. here: http://stackoverflow.com/q/11620914/2737715 – Alexander Vogt Dec 05 '15 at 13:00