您的位置:首页技术文章
文章详情页

python热力图实现简单方法

【字号: 日期:2022-06-28 17:39:33浏览:31作者:猪猪

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

1.导入相关的packages

import seaborn as sns%matplotlib inlinesns.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文件,可以放大和缩小。

import gmplot # plot the locations on google mapimport numpy as np # linear algebraimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv())import matplotlib.pyplot as plt # data visualizationimport seaborn as sns # data visualizationdf = pd.read_csv('data.csv')df = pd.DataFrame(df)df_td = pd.read_csv('datacopy.csv')df_td = pd.DataFrame(df_td)# print df.dtypesprint (df.shape)print (df_td.shape)def plot_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]) # Initialize the map to the first location in the list 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) # Write the map in an HTML file # gmap.draw(’Paths_map.html’) gmap.draw(’{}_Paths_map.html’.format(number))plot_heat_map(df,’4’)

内容扩展:

实例扩展1

# -*- coding: utf-8 -*-from pyheatmap.heatmap import HeatMapimport numpy as npN = 10000X = np.random.rand(N) * 255 # [0, 255]Y = np.random.rand(N) * 255data = []for i in range(N): tmp = [int(X[i]), int(Y[i]), 1] data.append(tmp)heat = HeatMap(data)heat.clickmap(save_as='1.png') #点击图heat.heatmap(save_as='2.png') #热图

实例扩展2

import matplotlib.pyplot as pltimport matplotlib.cm as cmfrom matplotlib.colors import LogNormimport numpy as npx, y = np.random.rand(10), np.random.rand(10)z = (np.random.rand(9000000)+np.linspace(0,1, 9000000)).reshape(3000, 3000)plt.imshow(z+10, extent=(np.amin(x), np.amax(x), np.amin(y), np.amax(y)), cmap=cm.hot, norm=LogNorm())plt.colorbar()plt.show()

以上就是python热力图实现简单方法的详细内容,更多关于python热力图的原理实现的资料请关注好吧啦网其它相关文章!

标签: Python 编程
相关文章: