python热力图的原理实现
发布时间:2024-09-12 12:16:19

当我们想判断不同的变量时,我们会分析它们之间的联系。这个概念也被用于实例生活中,最常见的是制作一个地理热图。很多人对绘制热图的方法不是很清楚。我们可以安装相关工具,了解一些使用参数,然后在实例中绘制热图的实例体验。让我们来看看具体的方法。
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相对简单,导入gooonGlmap然后将经纬度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热力图的原理,您可以先跟随代码进行测试,看看是否可以运行相关的热力图,然后学习一些知识点。
