You may indeed use tripcolor
to plot a set of triangles. In the code below the function quatromatrix
takes 4 2D arrays of values to colormap as input, creates the triangles and rearanges the colors to fit to the respective positions. It is thus very similar to plotting 4 imshow plots.
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")