python熱力圖實現(xiàn)簡單方法
在我們想要對不同變量進(jìn)行判斷的時候,會分析其中的之間的聯(lián)系。這種理念同樣也被用在實例生活中,最常見到的是做一個地理的熱力圖。很多人對畫熱力圖的方法不是很清楚,我們可以先裝好相關(guān)的工具,了解一些使用參數(shù),然后在實例中進(jìn)行畫熱力圖的實例體驗,下面就來看看具體的方法吧。
1.導(dǎo)入相關(guān)的packagesimport seaborn as sns%matplotlib inlinesns.set(font_scale=1.5)2.參數(shù)
vmax:設(shè)置顏色帶的最大值
vmin:設(shè)置顏色帶的最小值
cmap:設(shè)置顏色帶的色系
center:設(shè)置顏色帶的分界線
annot:是否顯示數(shù)值注釋
fmt:format的縮寫,設(shè)置數(shù)值的格式化形式
linewidths:控制每個小方格之間的間距
linecolor:控制分割線的顏色
cbar_kws:關(guān)于顏色帶的設(shè)置
mask:傳入布爾型矩陣,若為矩陣內(nèi)為True,則熱力圖相應(yīng)的位置的數(shù)據(jù)將會被屏蔽掉(常用在繪制相關(guān)系數(shù)矩陣圖)
3.實例用Python生成heatmap比較簡單,導(dǎo)入googlmap然后把經(jīng)緯度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’)
內(nèi)容擴(kuò)展:
實例擴(kuò)展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') #熱圖
實例擴(kuò)展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熱力圖實現(xiàn)簡單方法的詳細(xì)內(nèi)容,更多關(guān)于python熱力圖的原理實現(xiàn)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Spring如何集成ibatis項目并實現(xiàn)dao層基類封裝2. IDEA 2020.1.2 安裝教程附破解教程詳解3. idea設(shè)置提示不區(qū)分大小寫的方法4. 使用AJAX(包含正則表達(dá)式)驗證用戶登錄的步驟5. 利用ajax+php實現(xiàn)商品價格計算6. IntelliJ IDEA導(dǎo)出項目的方法7. Java利用TCP協(xié)議實現(xiàn)客戶端與服務(wù)器通信(附通信源碼)8. Java PreparedStatement用法詳解9. django queryset相加和篩選教程10. JS圖片懶加載庫VueLazyLoad詳解

網(wǎng)公網(wǎng)安備