Python-Seaborn热图绘制

制图环境:
pycharm
python-3.6
Seaborn-0.8

热图

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
np.random.seed(0)
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
plt.show()

Python-Seaborn热图绘制_第1张图片

# 改变颜色映射的值范围
ax = sns.heatmap(uniform_data, vmin=0, vmax=1)
plt.show()

Python-Seaborn热图绘制_第2张图片

uniform_data = np.random.randn(10, 12)
#为以0为中心的数据绘制一张热图
ax = sns.heatmap(uniform_data, center=0)
plt.show()

Python-Seaborn热图绘制_第3张图片

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
#用行和列标签绘制
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# 绘制x-y-z的热力图,比如 年-月-销量 的热力图
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, ax=ax)
#设置坐标字体方向
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='right')
label_x = ax.get_xticklabels()
plt.setp(label_x, rotation=45, horizontalalignment='right')
plt.show()

Python-Seaborn热图绘制_第4张图片

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# 绘制x-y-z的热力图,比如 年-月-销量 的热力图
f, ax = plt.subplots(figsize=(9, 6))
#使用不同的颜色
sns.heatmap(flights, fmt="d",cmap='YlGnBu', ax=ax)
#设置坐标字体方向
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='right')
label_x = ax.get_xticklabels()
plt.setp(label_x, rotation=45, horizontalalignment='right')
plt.show()

Python-Seaborn热图绘制_第5张图片

注释热图

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# 绘制x-y-z的热力图,比如 年-月-销量 的热力图
f, ax = plt.subplots(figsize=(9, 6))
#绘制热力图,还要将数值写到热力图上
sns.heatmap(flights, annot=True, fmt="d", ax=ax)
#设置坐标字体方向
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='right')
label_x = ax.get_xticklabels()
plt.setp(label_x, rotation=45, horizontalalignment='right')
plt.show()

Python-Seaborn热图绘制_第6张图片

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# 绘制x-y-z的热力图,比如 年-月-销量 的热力图
f, ax = plt.subplots(figsize=(9, 6))
#绘制热力图,还要将数值写到热力图上
#每个网格上用线隔开
sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)
#设置坐标字体方向
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='right')
label_x = ax.get_xticklabels()
plt.setp(label_x, rotation=45, horizontalalignment='right')
plt.show()

Python-Seaborn热图绘制_第7张图片

聚类热图

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
# 绘制x-y-z的热力图,比如 年-月-销量 的聚类热图
g= sns.clustermap(flights, fmt="d",cmap='YlGnBu')
ax = g.ax_heatmap
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='left')
plt.show()

Python-Seaborn热图绘制_第8张图片

import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
iris = sns.load_dataset("iris")
species = iris.pop("species")
#设置图片大小
g= sns.clustermap(iris, fmt="d",cmap='YlGnBu',figsize=(6,9))
ax = g.ax_heatmap
label_y = ax.get_yticklabels()
plt.setp(label_y, rotation=360, horizontalalignment='left')
#设置图片名称,分辨率,并保存
plt.savefig('cluster.tif',dpi = 300)
plt.show()

Python-Seaborn热图绘制_第9张图片

注:更多参数的用法请查阅官方文档

你可能感兴趣的:(python数据挖掘)