虽然这是一个老问题,但我最近做了一些相关的事情:在同一个图中绘制两个热图。我通过将正方形转换为散点图来做到这一点,我将正方形转换为两个三角形。
我使用自定义标记制作了两个三角形:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
def getCustomSymbol1(path_index=1):
if path_index==1: #upper triangle
verts = [
(0.0,0.0),
(1.0,0.0),
(1.0,1.0),
(0.0,0.0),]
else: #lower triangle
verts = [
(0.0,0.0),
(0.0,1.0),
(1.0,1.0),
(0.0,0.0),]
codes = [matplotlib.path.Path.MOVETO,
matplotlib.path.Path.LINETO,
matplotlib.path.Path.LINETO,
matplotlib.path.Path.CLOSEPOLY,
]
pathCS1 = matplotlib.path.Path(verts, codes)
return pathCS1, verts
def plot_mat(matrix=np.random.rand(20,20), path_index=1, alpha=1.0, vmin=0., vmax=1.):
nx,ny = matrix.shape
X,Y,values = zip(*[ (i,j,matrix[i,j]) for i in range(nx) for j in range(ny) ] )
marker,verts = getCustomSymbol1(path_index=path_index)
ax.scatter(X,Y,s=4000,
marker=marker,
c=values,
cmap='viridis',
alpha=alpha,
vmin=vmin, vmax=vmax )
return
fig = plt.figure()
ax = fig.add_subplot(111)
A = np.random.uniform(20,50,30).reshape([6,5])
B = np.random.uniform(40,70,30).reshape([6,5])
vmin = np.min([A,B])
vmax = np.max([A,B])
plot_mat(path_index=1,vmin=vmin,vmax=vmax,matrix=A)
plot_mat(path_index=2,vmin=vmin,vmax=vmax,matrix=B)
plt.xlim([0,6])
plt.ylim([0,5])
# for the colorbar i did the trick to make first a fake mappable:
sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=vmin, vmax=vmax ) )
sm._A=[]
plt.colorbar(sm)
plt.show()
这在一起可以给你类似的东西: