我正在使用 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.figure
量DATA
?
图形的宽度应该始终相同,宽度DATA
不会改变,只有高度。