seaborn.heatmap操作手册

本文转自seaborn.heatmap官方操作手册:http://seaborn.pydata.org/generated/seaborn.heatmap.html

heatmap很好,很强大!

seaborn.heatmap

seaborn0.9.0

seaborn.heatmap(datavmin=Nonevmax=Nonecmap=Nonecenter=Nonerobust=Falseannot=Nonefmt='.2g'annot_kws=Nonelinewidths=0linecolor='white'cbar=Truecbar_kws=Nonecbar_ax=Nonesquare=Falsexticklabels='auto'yticklabels='auto'mask=Noneax=None**kwargs)

Plot rectangular data as a color-encoded matrix.

This is an Axes-level function and will draw the heatmap into the currently-active Axes if none is provided to the ax argument. Part of this Axes space will be taken and used to plot a colormap, unless cbar is False or a separate Axes is provided to cbar_ax.

Parameters:

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.

Returns:

ax : matplotlib Axes

Axes object with the heatmap.

Examples

Plot a heatmap for a numpy array:

>>> import numpy as np; np.random.seed(0)
>>> import seaborn as sns; sns.set()
>>> uniform_data = np.random.rand(10, 12)
>>> ax = sns.heatmap(uniform_data)

../_images/seaborn-heatmap-1.png

Change the limits of the colormap:

>>> ax = sns.heatmap(uniform_data, vmin=0, vmax=1)

../_images/seaborn-heatmap-2.png

Plot a heatmap for data centered on 0 with a diverging colormap:

>>> normal_data = np.random.randn(10, 12)
>>> ax = sns.heatmap(normal_data, center=0)

../_images/seaborn-heatmap-3.png

Plot a dataframe with meaningful row and column labels:

>>> flights = sns.load_dataset("flights")
>>> flights = flights.pivot("month", "year", "passengers")
>>> ax = sns.heatmap(flights)

../_images/seaborn-heatmap-4.png

Annotate each cell with the numeric value using integer formatting:

>>> ax = sns.heatmap(flights, annot=True, fmt="d")

../_images/seaborn-heatmap-5.png

Add lines between each cell:

>>> ax = sns.heatmap(flights, linewidths=.5)

../_images/seaborn-heatmap-6.png

Use a different colormap:

>>> ax = sns.heatmap(flights, cmap="YlGnBu")

../_images/seaborn-heatmap-7.png

Center the colormap at a specific value:

>>> ax = sns.heatmap(flights, center=flights.loc["January", 1955])

../_images/seaborn-heatmap-8.png

Plot every other column label and don’t plot row labels:

>>> data = np.random.randn(50, 20)
>>> ax = sns.heatmap(data, xticklabels=2, yticklabels=False)

../_images/seaborn-heatmap-9.png

Don’t draw a colorbar:

>>> ax = sns.heatmap(flights, cbar=False)

../_images/seaborn-heatmap-10.png

Use different axes for the colorbar:

>>> grid_kws = {"height_ratios": (.9, .05), "hspace": .3}
>>> f, (ax, cbar_ax) = plt.subplots(2, gridspec_kw=grid_kws)
>>> ax = sns.heatmap(flights, ax=ax,
...                  cbar_ax=cbar_ax,
...                  cbar_kws={"orientation": "horizontal"})

../_images/seaborn-heatmap-11.png

Use a mask to plot only part of a matrix

>>> corr = np.corrcoef(np.random.randn(10, 200))
>>> mask = np.zeros_like(corr)
>>> mask[np.triu_indices_from(mask)] = True
>>> with sns.axes_style("white"):
...     ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True)

../_images/seaborn-heatmap-12.png

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值