0

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

user6253673
  • 3
  • 1
  • 4
  • http://stackoverflow.com/questions/2369492/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set – Hun Apr 25 '16 at 23:27
  • Thanks, but this seems to still be a regular heatmap where everything is done by frequency of (x,y) points instead of using color for the attached values and using intensity for frequency instead – user6253673 Apr 25 '16 at 23:30
  • Can you post some of your data as an example? – Hun Apr 25 '16 at 23:31
  • value-to-color is standard (you can pass an array of colors in with an array of x and an array of y values). If you set the transparency of each point to 0.5, say, then when they're frequent enough to overlap the color will become more intense/less transparent. – cphlewis Apr 25 '16 at 23:45
  • edited to include more info – user6253673 Apr 25 '16 at 23:46
  • It works for a regular scatter plot, but I was wondering if it'd be possible to implement in a heatmap-style figure – user6253673 Apr 25 '16 at 23:47
  • You'd have to be confident that the spatial units you were using as the cells of the heatmap were statistically/pedegogically meaningful. Aggregating (x,y) points correctly into cells can be tricky when the underlying field isn't homogeneous (which roads/cities sure aren't!) – cphlewis Apr 26 '16 at 00:18

2 Answers2

2

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()
T. Silver
  • 372
  • 1
  • 6
  • is there a way to do this where instead of counting point density you instead calculate the density as of the values of nearby points? – Skyler Nov 10 '19 at 04:17
1

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.

Oru
  • 55
  • 8