基于matplotlib的数据可视化 - 热图imshow

2023-06-05,,

热图

Display an image on the axes.

可以用来比较两个矩阵的相似程度

mp.imshow(z, cmap=颜色映射,origin=垂直轴向)

imshow(
    X,
    cmap=None,
    norm=None,
    aspect=None,
    interpolation=None,
    alpha=None,
    vmin=None,
    vmax=None,
    origin=None,
    extent=None,
    shape=None,
    filternorm=1,
    filterrad=4.0,
    imlim=None,
    resample=None,
    url=None,
    hold=None,
    data=None,
    **kwargs
)

X - array_like, shape (n, m) or (n, m, 3) or (n, m, 4);Display the image in `X` to current axes.

X 可以是数组array,或PIL image,若为数组,它following shapes and types

- M * N -- values to be mapped (float or int);该数组形式基于norm(将标量映射到标量 mapping scalar to scalar)和 cmap(将标准标量映射到颜色mapping the normed scalar to a color)

- M * N * 3 -- RGB (float or uint8)

- M * N * 4 -- RGBA (float or uint8)

RGB和RGBA阵列的元素表示M * N图像的像素。 对于浮点数,所有值应在[0 .. 1]的范围内,对于整数,所有值应在[0 ... 255]的范围内。 超出范围的值将被剪切到这些边界。

cmap - optional, default: None

aspect - ['auto' | 'equal' | scalar], optional, default: None

auto - 则更改图像宽高比以匹配轴的宽高比。
    equal - If 'equal', and `extent` is None, 则更改轴纵横比以匹配图像的纵横比。 If `extent` is not `None`, 则更改轴纵横比以匹配范围。

interpolation - string, optional, default: None ,

Acceptable values are 'none', 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc',
'lanczos'

norm -  : `~matplotlib.colors.Normalize`, optional, default: None 略

vmin, vmax - scalar, optional, default: None

`vmin`和`vmax`与norm结合使用以标准化亮度数据。 注意,如果传递一个`norm`实例则`vmin`和`vmax`的设置将被忽略。

alpha - scalar, optional, default: None

介于0(透明)和1(不透明)之间。RGBA input data 时 alpha 参数自动忽略

origin : ['upper' | 'lower'], optional, default: None

将数组的[0,0]索引放在轴的左上角( upper) 或左下角( lower) 。 如果为None,则默认为rc`mage.origin`。

extent : scalars (left, right, bottom, top), optional, default: None

数据坐标中左下角和右上角的位置。 如果为“无”,则定位图像使得像素中心落在基于零的(行,列)索引上。

shape : scalars (columns, rows), optional, default: None

For raw buffer images

filternorm - scalar, optional, default: 1

filterrad - scalar, optional, default: 4.0

示例

import numpy as np
import matplotlib.pyplot as plt

n = 1000
# 用meshgrid生成一个二维数组
x, y = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 3, n))
z = (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 - y**2)

# 画图
plt.figure('Hot', facecolor='lightgray')
plt.title('hotshot', fontsize=20)
plt.xlabel('x', fontsize=14)
plt.ylabel('y', fontsize=14)
plt.tick_params(labelsize=10)
plt.grid(linestyle=':')

plt.imshow(z, cmap='jet', origin='low')
plt.colorbar().set_label('z', fontsize=14)

plt.show()

官方网站 Image tutorial 、

Matplotlib调用imshow()函数绘制热图

matplotlib热图

基于matplotlib的数据可视化 - 热图imshow的相关教程结束。

《基于matplotlib的数据可视化 - 热图imshow.doc》

下载本文的Word格式文档,以方便收藏与打印。