1

使用标准 matshow 示例:

from matplotlib.pylab import *
dim = (12,12)
aa = zeros(dim)
for i in range(min(dim)):
    aa[i,i] = i
matshow(aa)
show()

例子
(来源:matplotlib.org

如何控制每一行的高度?

在我的情况下,行索引(即国家/地区)可以用非线性间距(例如 GDP)表示以表示大小,我想通过改变缩放向量的行高来表示。(即如果有 12 行,那么在均匀分布的情况下,每行的行高为 1/12,由 [0.083, 0.083, ...., 0.083] 表示,那么不均匀的行高可以由总和为的任何向量设置1)

4

1 回答 1

5

您可以使用 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]

在此处输入图像描述

于 2013-07-03T08:59:20.730 回答