使用seaborn绘制漂亮的热度图

还是使用jupyter notebook作为开发环境,首先引入所必须的包以及对环境进行相应设置。

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# 调用魔法方法 使得每次显示结果时不用调用plt.show()方法
sns.set(style='whitegrid', color_codes=True)
# 设置默认风格 
np.random.seed(sum(map(ord, 'categorical')))
# 当我们设置相同的seed,每次生成的随机数相同。
# 如果不设置seed,则每次会生成不同的随机数
# map() 会根据提供的函数对指定序列做映射。
# 即返回'distributions'该字符串每个字母的二进制数
# 并用sum函数求和,将和作为种子,这里似乎是习惯上的写法。

首先先产生一组3*3矩阵,并调用sns的heatmap()方法绘制一个简单的热度图。

data = np.random.rand(3, 3)
sns.heatmap(data)

结果如下:
这里写图片描述
可以通过右边的小竖线,看出来该3*3矩阵的数据大概分布规律。

ndata = np.random.randn(3,3)
print(ndata)
sns.heatmap(ndata, center=0)

我们还可以通过center参数设置中间值,结果如下:

[ [-1.61598034 1.2296366 0.82214517]
[-0.39834485 -0.97147653 -0.5531709 ]
[ 0.26034732 -0.03442802 0.96284724] ]

这里写图片描述

载入内置数据集:

flights = sns.load_dataset('flights')
flights.head()

year month passengers
0 1949 January 112
1 1949 February 118
2 1949 March 132
3 1949 April 129
4 1949 May 121

# pivot() 可以将dataframe转换为行列式矩阵 并指定每个元素的存储值
flights = flights.pivot(index='month', columns='year',  values='passengers')
# print(flights)
plt.figure(figsize=(10,6))
ax = sns.heatmap(flights, fmt='d', linewidths=.5)
# fmt设置字体模式  linewidth设置每个小方格的间距 线宽

结果如图:
这里写图片描述
我们还可以设置绘图板的颜色模式:

plt.figure(figsize=(10,6))
ax = sns.heatmap(flights, fmt='d', linewidths=.5, cmap='YlGnBu')

结果如下:
这里写图片描述

heatmap函数具体参数如下:

data : rectangular dataset 绘制的数据集 多以矩阵

2D dataset that can be coerced into an ndarray. If a Pandas DataFrame is provided, the index/column information will be used to label the columns and rows.

vmin, vmax : floats, optional 开始与结束的范围 即右边小竖线的范围

Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments.

cmap : matplotlib colormap name or object, or list of colors, optional 显示的绘图板

The mapping from data values to color space. If not provided, the default will depend on whether center is set.

center : float, optional

The value at which to center the colormap when plotting divergant data. Using this parameter will change the default cmap if none is specified.

robust : bool, optional

If True and vmin or vmax are absent, the colormap range is computed with robust quantiles instead of the extreme values.

annot : bool or rectangular dataset, optional

If True, write the data value in each cell. If an array-like with the same shape as data, then use this to annotate the heatmap instead of the raw data.

fmt : string, optional

String formatting code to use when adding annotations.

annot_kws : dict of key, value mappings, optional

Keyword arguments for ax.text when annot is True.

linewidths : float, optional

Width of the lines that will divide each cell.

linecolor : color, optional

Color of the lines that will divide each cell.

cbar : boolean, optional

Whether to draw a colorbar.

cbar_kws : dict of key, value mappings, optional

Keyword arguments for fig.colorbar.

cbar_ax : matplotlib Axes, optional

Axes in which to draw the colorbar, otherwise take space from the main Axes.

square : boolean, optional

If True, set the Axes aspect to “equal” so each cell will be square-shaped.

xticklabels, yticklabels : “auto”, bool, list-like, or int, optional

If True, plot the column names of the dataframe. If False, don’t plot the column names. If list-like, plot these alternate labels as the xticklabels. If an integer, use the column names but plot only every n label. If “auto”, try to densely plot non-overlapping labels.

mask : boolean array or DataFrame, optional

If passed, data will not be shown in cells where mask is True. Cells with missing values are automatically masked.

ax : matplotlib Axes, optional

Axes in which to draw the plot, otherwise use the currently-active Axes.

kwargs : other keyword arguments

All other keyword arguments are passed to ax.pcolormesh.
  • 6
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangbowj123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值