Seaborn 热图 – 综合指南
热图被定义为数据的图形表示,使用颜色来可视化矩阵的值。在这种情况下,为了表示更常见的值或更高的活性,使用较亮的颜色,基本上使用红色,而为了表示不太常见或活性值,则优选较深的颜色。热图也由着色矩阵的名称定义。 Seaborn 中的热图可以使用 seaborn.heatmap()函数绘制。
seaborn.heatmap()
Syntax: seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs)
Important Parameters:
- data: 2D dataset that can be coerced into an ndarray.
- vmin, vmax: Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments.
- cmap: The mapping from data values to color space.
- center: The value at which to center the colormap when plotting divergent data.
- annot: If True, write the data value in each cell.
- fmt: String formatting code to use when adding annotations.
- linewidths: Width of the lines that will divide each cell.
- linecolor: Color of the lines that will divide each cell.
- cbar: Whether to draw a colorbar.
All the parameters except data are optional.
Returns: An object of type matplotlib.axes._subplots.AxesSubplot
让我们通过示例了解热图。
基本热图
使用默认参数制作热图。我们将使用 NumPy 模块的randint()函数创建一个 10×10 的二维数据。
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low = 1,
high = 100,
size = (10, 10))
print("The data to be plotted:\n")
print(data)
# plotting the heatmap
hm = sn.heatmap(data = data)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
vmin = 30
vmax = 70
# plotting the heatmap
hm = sn.heatmap(data=data,
vmin=vmin,
vmax=vmax)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
cmap = "tab20"
# plotting the heatmap
hm = sn.heatmap(data=data,
cmap=cmap)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
cmap = "tab20"
center = 0
# plotting the heatmap
hm = sn.heatmap(data=data,
cmap=cmap,
center=center)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
annot = True
# plotting the heatmap
hm = sn.heatmap(data=data,
annot=annot)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
linewidths = 2
linecolor = "yellow"
# plotting the heatmap
hm = sn.heatmap(data=data,
linewidths=linewidths,
linecolor=linecolor)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
cbar = False
# plotting the heatmap
hm = sn.heatmap(data=data,
cbar=cbar)
# displaying the plotted heatmap
plt.show()
Python3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
xticklabels = False
yticklabels = False
# plotting the heatmap
hm = sn.heatmap(data=data,
xticklabels=xticklabels,
yticklabels=yticklabels)
# displaying the plotted heatmap
plt.show()
输出:
The data to be plotted:
[[46 30 55 86 42 94 31 56 21 7]
[68 42 95 28 93 13 90 27 14 65]
[73 84 92 66 16 15 57 36 46 84]
[ 7 11 41 37 8 41 96 53 51 72]
[52 64 1 80 33 30 91 80 28 88]
[19 93 64 23 72 15 39 35 62 3]
[51 45 51 17 83 37 81 31 62 10]
[ 9 28 30 47 73 96 10 43 30 2]
[74 28 34 26 2 70 82 53 97 96]
[86 13 60 51 95 26 22 29 14 29]]
我们将在所有示例中使用相同的数据。
锚定颜色图
如果我们将vmin值设置为 30,将vmax值设置为 70,那么只会显示值在 30 到 70 之间的单元格。这称为锚定颜色图。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
vmin = 30
vmax = 70
# plotting the heatmap
hm = sn.heatmap(data=data,
vmin=vmin,
vmax=vmax)
# displaying the plotted heatmap
plt.show()
输出:
选择颜色图
在此,我们将查看cmap参数。 Matplotlib 为我们提供了多个颜色图,您可以在此处查看所有颜色图。在我们的示例中,我们将使用tab20 。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
cmap = "tab20"
# plotting the heatmap
hm = sn.heatmap(data=data,
cmap=cmap)
# displaying the plotted heatmap
plt.show()
输出:
将颜色图居中
通过将center参数传递为 0 将 cmap 居中到 0。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
cmap = "tab20"
center = 0
# plotting the heatmap
hm = sn.heatmap(data=data,
cmap=cmap,
center=center)
# displaying the plotted heatmap
plt.show()
输出:
显示单元格值
如果我们想显示单元格的值,那么我们将参数annot传递为 True。 fmt用于选择显示单元格内容的数据类型。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
annot = True
# plotting the heatmap
hm = sn.heatmap(data=data,
annot=annot)
# displaying the plotted heatmap
plt.show()
输出:
自定义分隔线
我们可以分别使用linewidths和linecolor参数更改分隔单元格的线条的粗细和颜色。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
linewidths = 2
linecolor = "yellow"
# plotting the heatmap
hm = sn.heatmap(data=data,
linewidths=linewidths,
linecolor=linecolor)
# displaying the plotted heatmap
plt.show()
输出:
隐藏颜色条
我们可以通过将cbar参数设置为 False 来禁用颜色栏。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
cbar = False
# plotting the heatmap
hm = sn.heatmap(data=data,
cbar=cbar)
# displaying the plotted heatmap
plt.show()
输出:
去除标签
我们可以通过分别在xticklabels和yticklabels参数中传递 False 来禁用 x-label 和 y-label。
蟒蛇3
# importing the modules
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# generating 2-D 10x10 matrix of random numbers
# from 1 to 100
data = np.random.randint(low=1,
high=100,
size=(10, 10))
# setting the parameter values
xticklabels = False
yticklabels = False
# plotting the heatmap
hm = sn.heatmap(data=data,
xticklabels=xticklabels,
yticklabels=yticklabels)
# displaying the plotted heatmap
plt.show()
输出: