python繪制熱力圖heatmap
本文實(shí)例為大家分享了python繪制熱力圖的具體代碼,供大家參考,具體內(nèi)容如下
python的熱力圖是用皮爾遜相關(guān)系數(shù)來查看兩者之間的關(guān)聯(lián)性。
#encoding:utf-8
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib import axes
import pylab
pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']#防止中文亂碼
pylab.mpl.rcParams['axes.unicode_minus'] = False#防止中文亂碼
def draw_heatmap(data,xlabels,ylabels):
cmap = cm.Blues
figure=plt.figure(facecolor='w')
ax=figure.add_subplot(2,1,1,position=[0.1,0.15,0.8,0.8])
ax.set_yticks(range(len(ylabels)))
ax.set_yticklabels(ylabels)
ax.set_xticks(range(len(xlabels)))
ax.set_xticklabels(xlabels)
vmax=data[0][0]
vmin=data[0][0]
for i in data:
for j in i:
if j>vmax:
vmax=j
if j<vmin:
vmin=j
map=ax.imshow(data,interpolation='nearest',cmap=cmap,aspect='auto',vmin=vmin,vmax=vmax)
cb = plt.colorbar(mappable=map,cax=None,ax=None,shrink=0.5)
plt.xticks(rotation=90) # 將字體進(jìn)行旋轉(zhuǎn)
plt.yticks(rotation=360)
plt.show()
data = pd.read_csv('test.csv',encoding='gbk')
a = [[1063620,291288,213322,120233,972752,1896180,483012,1609664,413538,778350,420643,212472,2599510,1574470,254141],[258914,48064,31948,19534,142792,295841,69143,291524,78926,90238,79336,47938,454656,271486,35304],[517687,135483,68418,66670,301544,777798,307562,810314,234086,238859,145959,125258,1480672,764612,153237],[277377,38581,31145,17612,121162,254534,60746,253148,62054,93499,63346,36422,356036,212109,27758],[19030,2835,2174,1575,7325,18258,6837,23457,5340,5277,5120,4017,34122,21314,2961],[351720,107299,57186,55485,337368,563436,188368,563515,128047,178664,117886,72451,798121,444825,65599]]
xlabels= [u'3C電子',u'房產(chǎn)家居',u'服飾',u'健康保健',u'金融財(cái)經(jīng)',u'旅游',u'美容美體',u'汽車',u'求職&教育',u'奢侈品',u'體育健身',u'網(wǎng)游',u'休閑&愛好',u'影視娛樂',u'孕嬰育兒']
ylabels= ['iphoneX','mix2','oppor11','samsang','vivo','mate10']
draw_heatmap(a,xlabels,ylabels)
結(jié)果:

本文已被收錄到專題《python圖片處理操作》 ,歡迎大家點(diǎn)擊學(xué)習(xí)更多精彩內(nèi)容。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)數(shù)值交換的四種方式
本文介紹了Python中四種實(shí)現(xiàn)數(shù)值交換的方法,包括使用臨時(shí)變量、元組解包、列表和異或運(yùn)算,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
Python中數(shù)據(jù)清洗與處理的常用方法小結(jié)
數(shù)據(jù)清洗與處理是數(shù)據(jù)分析的重要步驟,確保數(shù)據(jù)的準(zhǔn)確性和一致性,這篇文章為大家整理了Python中一些常用的數(shù)據(jù)清洗與處理方法,需要的可以參考下2025-02-02
pandas實(shí)現(xiàn)將dataframe滿足某一條件的值選出
今天小編就為大家分享一篇pandas實(shí)現(xiàn)將dataframe滿足某一條件的值選出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Django app配置多個(gè)數(shù)據(jù)庫代碼實(shí)例
這篇文章主要介紹了Django app配置多個(gè)數(shù)據(jù)庫代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Python 內(nèi)置函數(shù)globals()和locals()對(duì)比詳解
這篇文章主要介紹了Python globals()和locals()對(duì)比詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

