想要改变世界,就得先改变自己。 ------ 博客首页

5-7 可视化库Seaborn-热度图绘制

In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

np.random.seed(0)
sns.set()
 

画热力图

In [2]:
uniform_data=np.random.rand(3,3)
#打印数据
print(uniform_data)
#画热力图
heatmap=sns.heatmap(uniform_data)
 
[[0.5488135  0.71518937 0.60276338]
 [0.54488318 0.4236548  0.64589411]
 [0.43758721 0.891773   0.96366276]]
 
 

1.定义colorbar范围

In [3]:
ax=sns.heatmap(uniform_data,vmin=0.2,vmax=0.5)
 
 

2.center:指定以...为中心,两端极限变色

In [4]:
normal_data=np.random.randn(3,3)
print(normal_data)
ax=sns.heatmap(normal_data,center=0)
 
[[ 1.26611853 -0.50587654  2.54520078]
 [ 1.08081191  0.48431215  0.57914048]
 [-0.18158257  1.41020463 -0.37447169]]
 
In [5]:
flight=sns.load_dataset("flights")
print(flight.head())
#画图
flight=flight.pivot("month","year","passengers")#定义变量
print(flight)
ax=sns.heatmap(flight)
 
   year     month  passengers
0  1949   January         112
1  1949  February         118
2  1949     March         132
3  1949     April         129
4  1949       May         121
year       1949  1950  1951  1952  1953  1954  1955  1956  1957  1958  1959  \
month                                                                         
January     112   115   145   171   196   204   242   284   315   340   360   
February    118   126   150   180   196   188   233   277   301   318   342   
March       132   141   178   193   236   235   267   317   356   362   406   
April       129   135   163   181   235   227   269   313   348   348   396   
May         121   125   172   183   229   234   270   318   355   363   420   
June        135   149   178   218   243   264   315   374   422   435   472   
July        148   170   199   230   264   302   364   413   465   491   548   
August      148   170   199   242   272   293   347   405   467   505   559   
September   136   158   184   209   237   259   312   355   404   404   463   
October     119   133   162   191   211   229   274   306   347   359   407   
November    104   114   146   172   180   203   237   271   305   310   362   
December    118   140   166   194   201   229   278   306   336   337   405   

year       1960  
month            
January     417  
February    391  
March       419  
April       461  
May         472  
June        535  
July        622  
August      606  
September   508  
October     461  
November    390  
December    432  
 
 
  • 在heatmap中加入显示具体的数值:annot=True表示显示数值,fmt="d"编码的格式设置,默认是科学计数法
In [6]:
ax=sns.heatmap(flight,annot=True,fmt="d")
 
 
  • 加格子linewidths
In [7]:
ax=sns.heatmap(flight,linewidths=0.5)
 
 
  • 设置颜色cmap
In [8]:
ax=sns.heatmap(flight,cmap="YlGnBu")
 
posted @ 2019-10-30 20:40  karina512  阅读(457)  评论(0编辑  收藏  举报