user6253673
user6253673

Reputation: 27

Generating a heatmap with a scatter data set

I have a set of (x, y) data points, and each point has an attached value to it. I'd like to create a heatmap that uses the attached value to determine color, and uses color intensity/transparency to determine frequency.

Is there anyway I can implement this with matplotlib? Thanks!

Edit: I am working with traffic accident data. The (x,y) points are just latitude and longitude pairs, and each of these has a corresponding value for the severity of the accident. The ideal plot would use color to represent severity, but then transparency to represent frequency of location points. For example, then places with a small accident number, but more high severity accidents will be a transparent red, but places with high accident numbers, but not many severe accidents will be a opaque blue

Upvotes: 0

Views: 6284

Answers (2)

T. Silver
T. Silver

Reputation: 372

You can use numpy.histogram2d. Just set x to the list of x values, y to the list of y values, and weights to the heat map values.

import numpy as np
import matplotlib.pyplot as plt

# make sure to set x, y, and weights here

heatmap, _, _ = np.histogram2d(x, y, weights=weights)

plt.clf()
plt.imshow(heatmap)
plt.show()

Upvotes: 2

Oru
Oru

Reputation: 55

Try this:

import matplotlib.pyplot as plt
from matplotlib import cm
f = plt.figure()
ax = f.add_subplot(111)
ax.pcolormesh(x,y,f(x,y), cmap = cm.Blues)
f.show()

You can see an example here http://matplotlib.org/examples/images_contours_and_fields/pcolormesh_levels.html

Let me know if you need more information. Or if I assumed something incorrectly.

Upvotes: 1

Related Questions