python热力图的原理实现

Python (164) 2023-07-09 08:35:49

在我们想要对不同变量进行判断的时候,会分析其中的之间的联系。这种理念同样也被用在实例生活中,最常见到的是做一个地理的热力图。很多人对画热力图的方法不是很清楚,我们可以先装好相关的工具,了解一些使用参数,然后在实例中进行画热力图的实例体验,下面就来看看具体的方法吧。

1.导入相关的packages

importseabornassns
%matplotlibinline
sns.set(font_scale=1.5)

2.参数

vmax:设置颜色带的值

vmin:设置颜色带的最小值

cmap:设置颜色带的色系

center:设置颜色带的分界线

annot:是否显示数值注释

fmt:format的缩写,设置数值的格式化形式

linewidths:控制每个小方格之间的间距

linecolor:控制分割线的颜色

cbar_kws:关于颜色带的设置

mask:传入布尔型矩阵,若为矩阵内为True,则热力图相应的位置的数据将会被屏蔽掉(常用在绘制相关系数矩阵图)

3.实例

用Python生成heatmap比较简单,导入googlmap然后把经纬度plot在地图上就可以了。最后把heatmap生成为一个html文件,可以放大和缩小。

importgmplot#plotthelocationsongooglemap
importnumpyasnp#linearalgebra
importpandasaspd#dataprocessing,CSVfileI/O(e.g.pd.read_csv())
importmatplotlib.pyplotasplt#datavisualization
importseabornassns#datavisualization


df=pd.read_csv("data.csv")
df=pd.DataFrame(df)
df_td=pd.read_csv("datacopy.csv")
df_td=pd.DataFrame(df_td)
#printdf.dtypes
print(df.shape)
print(df_td.shape)

defplot_heat_map(data,number):
latitude_array=data['INTPTLAT'].values
latitude_list=latitude_array.tolist()
print(latitude_list[0])

Longitude_array=data['INTPTLONG'].values
longitude_list=Longitude_array.tolist()
print(longitude_list[0])

#Initializethemaptothefirstlocationinthelist
gmap=gmplot.GoogleMapPlotter(latitude_list[0],longitude_list[0],10)

#gmap.scatter(latitude_list,longitude_list,edge_width=10)
gmap.heatmap(latitude_list,longitude_list)

#WritethemapinanHTMLfile
#gmap.draw('Paths_map.html')
gmap.draw('{}_Paths_map.html'.format(number))


plot_heat_map(df,'4')

以上就是python热力图的原理实现,大家可以先跟着代码试验一下,看看是否能运行出相关的热力图,然后就其中的一些知识点进行学习。

THE END

发表回复