熱力圖是一種數據的圖形化表示,具體而言,就是將二維數組中的元素用顏色表示。熱力圖之所以非常有用,是因為它能夠從整體視角上展示數據,更確切的說是數值型數據。
使用imshow()
函數可以非常容易地制作熱力圖。
1. 函數imshow()
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, **kwargs)
主要用到的參數含義如下:
-
X
可以使類似數組的對象,或者是PIL類型圖像,其中,數組對象可選shape為:(M, N)
單純的二維數組,元素是標量數據,會通過colormap
展示(M, N, 3)
RGB三通道圖像,元素值可以是\(0-1\)之間的float
或者\(0-255\)之間的int
(M, N, 4)
RGBA圖像,多出來的一維屬性,比如是透明度,其元素值和3通道的一樣,可以是\(0-1\)之間的float
或者\(0-255\)之間的int
※
M
代表rows
,N
代表colums
※ 超過元素限定范圍的元素值將被clipped
-
cmap
str
或matplotlib.colors.Colormap
類型,用於將標量數據映射到顏色的Colormap
實例或已注冊的Colormap
名稱。※ 只對二維數組有效,RGB(A)將自動忽略
-
norm
在使用cmap
之前,用來將二維數組數據歸一化到\([0, 1]\),默認是線性的,最小值對應\(0\),最大值對應\(1\)。這要注意,不然每次畫圖最大最小值不一樣,色彩不好比較。
-
interpolation
插值方法,默認'nearest'
,可以支持的方法有:'none'
'nearest'
'bilinear'
'bicubic'
'spline16'
'spline36'
'hanning'
'hamming'
'hermite'
'kaiser'
'quadric'
'catrom'
'gaussian'
'bessel'
'mitchell'
'sinc'
'lanczos'
-
alpha
透明度,\(0\)表示透明,\(1\)表示不透明 -
vmin
,vmax
當輸入的時二維數組標量數據並且沒有明確的norm
時,vmin
和vmax
定義colormap
覆蓋的數據范圍,默認情況下,colormap
覆蓋所提供的值的完整范圍數據當
norm
給定時,這兩個參數無效 -
origin
坐標軸的樣式,可選值為upper
和lower
,其對應坐標系樣式如下圖※
M
代表rows
,N
代表colums
2. 定制colorbars
2.1 基本連續colorbar繪制
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='horizontal', label='Some Units')

當然,也可以豎起來畫
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(1, 6))
fig.subplots_adjust(right=0.5)
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='vertical', label='Some Units')

2.2 離散間隔colorbar
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.colors.ListedColormap(['red', 'green', 'black', 'blue', 'cyan'])
cmap.set_over('0.25')
cmap.set_under('0.75')
bounds = [1, 2, 4, 5, 7, 8]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
fig.colorbar(
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
cax=ax,
boundaries=[0] + bounds + [13],
extend='both',
ticks=bounds,
spacing='proportional',
orientation='horizontal',
label='Discrete intervals, some other units',
)

2.3 帶有自定義擴展名長度的colorbar
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.colors.ListedColormap(['royalblue', 'cyan',
'yellow', 'orange'])
cmap.set_over('red')
cmap.set_under('blue')
bounds = [-1.0, -0.5, 0.0, 0.5, 1.0]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
fig.colorbar(
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
cax=ax,
boundaries=[-10] + bounds + [10],
extend='both',
extendfrac='auto',
ticks=bounds,
spacing='uniform',
orientation='horizontal',
label='Custom extension lengths, some other units',
)
plt.show()

3. 控制所有圖的colorbar和圖中元素對應顏色一致
import matplotlib as mpl
from matplotlib import pyplot as plt
fig, ax = plt.subplots(1, 1)
im = ax.imshow(data, interpolation="bicubic", vmin=vmin, vmax=vmax, cmap="jet")
fig.colorbar(im, ax=ax)
plt.show()
關鍵是要設置
vmin
和vmax