0

我正在使用 matplotlib 的 imshow 在 python 上制作地毯图。这是我的代码:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

DATA = np.array([[0,0,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 9,0,0,1,2,4,5,5,4,3,2,0],[ 0,0,0,1,2,4,5,5,4,3,2,0],[ 0,0,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0],[ 0,3,0,1,2,4,5,5,4,3,2,0] ])

#plt.imshow(DATA, interpolation='nearest', cmap=cm.bwr)
DATA.shape

fig = plt.figure(figsize=(5.5,6),dpi=300)
ax = fig.add_subplot(111)
plot = ax.imshow(DATA, interpolation='nearest', cmap=cm.rainbow)


cb = fig.colorbar(plot)
xlabel = ax.set_xlabel('Days')
ylabel= ax.set_ylabel('bla bla bla')
fig.savefig('carpet_test.png',bbox_extra_artists=[xlabel], bbox_inches='tight')

输出是这样的:

带有长数据的地毯图

但是,如果我缩短 DATA 并使用相同的代码,则输出为:

带有短数据的地毯图

1 和 2 之间的唯一区别是 的长度DATA。问题是,如何计算每个像素或点具有相同宽度和高度的figsize使用plt.figureDATA

图形的宽度应该始终相同,宽度DATA不会改变,只有高度。

4

1 回答 1

0

感谢@Francesco-montesano 的评论,我想出了一个解决方案。它并不完美,但它有效:

dshape=DATA.shape[0]
pixelsize = 0.8
figsize=np.array(dshape)*pixelsize



fig = plt.figure(figsize=(5.5,figsize),dpi=300)

有了它,我可以动态调整图形的高度。

于 2013-05-07T10:12:13.270 回答