您可以使用 pcolormesh(或 pcolor)创建由多边形组成的数组,这些可以具有您想要的任何形状。我认为像 matshow 或 imshow 这样的普通数组绘图将始终沿轴具有恒定的大小。
n = 6
# generate some data
gdp = np.array(np.random.randint(10,500,n))
countries = np.array(['Country%i' % (i+1) for i in range(n)])
matr = np.random.randint(0,10,(n,n))
# get the x and y arrays
y = np.insert(gdp.cumsum(),0,0)
xx,yy = np.meshgrid(np.arange(n+1),y)
# plot the matrix
fig, axs = plt.subplots(figsize=(6,6))
axs.pcolormesh(xx,yy,matr.T, cmap=plt.cm.Reds, edgecolors='k')
axs.set_ylim(y.min(),y.max())
# set the yticks + labels
axs.set_yticks(y[1:] - np.diff(y) / 2)
axs.set_yticklabels(countries)
#set xticks + labels
axs.xaxis.set_ticks_position('top')
axs.set_xticks(np.arange(n)+0.5)
axs.set_xticklabels(np.arange(n))
高度按以下比例缩放:
print countries
['Country1' 'Country2' 'Country3' 'Country4' 'Country5' 'Country6']
print gdp
[421 143 134 388 164 420]
