import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
a = np.random.poisson(lam=5, size=(10*5, 4*3))
fig, axes = plt.subplots(nrows=5, ncols=4+1, figsize=(6.5,7),
gridspec_kw={"width_ratios":4*[1] + [0.2]})
kw = dict(aspect="auto",vmin=a.min(), vmax= a.max())
for i in range(5):
for j in range(4):
im = axes[i,j].imshow(a[10*i:10*i+10,3*j:3*j+3], **kw )
axes[i,j].tick_params(axis=u'both', which=u'both',length=0)
axes[i,j].set_xticklabels([])
axes[i,j].set_yticklabels([])
ylabels=["gene {:02d}".format(i+1) for i in range(50)]
xlabels=["treatment {:02d}".format(i+1) for i in range(12)]
clabels=["{:02d}".format(i+1) for i in range(5)]
for i in range(5):
axes[i,4].set_facecolor(plt.cm.Set2(i/8.))
axes[i,4].text(0.5,.5, clabels[i], rotation=-90, color="w",
transform=axes[i,4].transAxes,
ha="center", va="center", fontweight="bold", fontsize=9)
axes[i,4].tick_params(axis=u'both', which=u'both',length=0)
axes[i,4].set_xticklabels([])
axes[i,4].set_yticklabels([])
axes[i,0].set_yticks(range(10))
axes[i,0].set_yticklabels(ylabels[i*10:i*10+10], fontsize=7)
for j in range(4):
axes[4,j].set_xticks(range(3))
axes[4,j].set_xticklabels(xlabels[j*3:j*3+3], fontsize=9, rotation=90)
axes[4,4].set_xticks([0.5])
axes[4,4].set_xticklabels(["category"], fontsize=9, rotation=90, fontweight="bold")
cax = fig.add_axes([0.9,0.5,0.03,0.44])
fig.colorbar(im, cax=cax)
plt.subplots_adjust(bottom=0.2, top=0.94, right=0.86)
plt.show()
下面是在matplotlib中复制上图的代码。在
如果不是所有的行或列都具有相同数量的数据点,但是如果数据点在一行或一列中都相同,则此解决方案仍然有效。然后需要调整gridspec的
^{pr2}$height_ratios
或width_ratios
,例如,如果第二列在x方向上有5个数据点,而不是3个,则需要调用当然,循环需要进行调整,以适应不同数量的滴答声等
相关问题 更多 >
编程相关推荐