您确实可以使用tripcolor 来绘制一组三角形。在下面的代码中,函数quatromatrix 将 4 个二维值数组作为输入进行颜色映射,创建三角形并重新排列颜色以适合各自的位置。因此,它与绘制 4 个 imshow 图非常相似。
import matplotlib.pyplot as plt
import numpy as np
def quatromatrix(left, bottom, right, top, ax=None, triplotkw={},tripcolorkw={}):
if not ax: ax=plt.gca()
n = left.shape[0]; m=left.shape[1]
a = np.array([[0,0],[0,1],[.5,.5],[1,0],[1,1]])
tr = np.array([[0,1,2], [0,2,3],[2,3,4],[1,2,4]])
A = np.zeros((n*m*5,2))
Tr = np.zeros((n*m*4,3))
for i in range(n):
for j in range(m):
k = i*m+j
A[k*5:(k+1)*5,:] = np.c_[a[:,0]+j, a[:,1]+i]
Tr[k*4:(k+1)*4,:] = tr + k*5
C = np.c_[ left.flatten(), bottom.flatten(),
right.flatten(), top.flatten() ].flatten()
triplot = ax.triplot(A[:,0], A[:,1], Tr, **triplotkw)
tripcolor = ax.tripcolor(A[:,0], A[:,1], Tr, facecolors=C, **tripcolorkw)
return tripcolor
right=np.random.randn(8, 8)
left=np.random.randn(8, 8)
bottom=np.random.randn(8, 8)
top=np.random.randn(8, 8)
fig, ax=plt.subplots()
quatromatrix(left, bottom, right, top, ax=ax,
triplotkw={"color":"k", "lw":1},
tripcolorkw={"cmap": "plasma"})
ax.margins(0)
ax.set_aspect("equal")