Python-Seaborn热图绘制的实现方法

时间:2021-05-22

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

热图

import numpy as npimport seaborn as snsimport matplotlib.pyplot as pltsns.set()np.random.seed(0)uniform_data = np.random.rand(10, 12)ax = sns.heatmap(uniform_data)plt.show()# 改变颜色映射的值范围ax = sns.heatmap(uniform_data, vmin=0, vmax=1)plt.show()uniform_data = np.random.randn(10, 12)#为以0为中心的数据绘制一张热图ax = sns.heatmap(uniform_data, center=0)plt.show()import matplotlib.pyplot as pltimport seaborn as snssns.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()import matplotlib.pyplot as pltimport seaborn as snssns.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()

注释热图

import matplotlib.pyplot as pltimport seaborn as snssns.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()import matplotlib.pyplot as pltimport seaborn as snssns.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()

聚类热图

import matplotlib.pyplot as pltimport seaborn as snssns.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_heatmaplabel_y = ax.get_yticklabels()plt.setp(label_y, rotation=360, horizontalalignment='left')plt.show()import matplotlib.pyplot as pltimport seaborn as snssns.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_heatmaplabel_y = ax.get_yticklabels()plt.setp(label_y, rotation=360, horizontalalignment='left')#设置图片名称,分辨率,并保存plt.savefig('cluster.tif',dpi = 300)plt.show()

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章