使用散点图数据生成热图

时间:2016-04-25 23:21:25

标签: python matplotlib

我有一组(x,y)数据点,每个点都有一个附加值。我想创建一个使用附加值来确定颜色的热图,并使用颜色强度/透明度来确定频率。

无论如何我可以用matplotlib实现这个吗?谢谢!

编辑:我正在处理交通事故数据。 (x,y)点只是纬度和经度对,并且每个点都具有事故严重性的相应值。理想的图表将使用颜色来表示严重性,然后使用透明度来表示位置点的频率。例如,那么事故数量较少的地方,但更严重的事故将是一个透明的红色,但事故数量较多的地方,但没有多少严重事故将是一个不透明的蓝色

2 个答案:

答案 0 :(得分:1)

试试这个:

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()

您可以在此处查看示例http://matplotlib.org/examples/images_contours_and_fields/pcolormesh_levels.html

如果您需要更多信息,请与我们联系。或者如果我做错了什么。

答案 1 :(得分:1)

您可以使用 numpy.histogram2d 。只需将x设置为x值列表,将y设置为y值列表,将weights设置为热图值。

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()