使用matplotlib进行奇怪的子图效果

时间:2013-04-03 15:23:28

标签: python matplotlib

我尝试按如下方式绘制数据:

第一个子剧情

alpha = ['Joy', 'fear', 'sadness', 'thankful','anger','surprise','love']
fig = pl.figure()
ax = fig.add_subplot(511,title='SGD')
cax = ax.matshow(cm)
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

稍后使用新cm的第二个子图:

ax = fig.add_subplot(521,title='LIBLINEAR')
cax = ax.matshow(cm)
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

第三个子图,稍后将使用新的cm:

ax = fig.add_subplot(512,title='MNB')
cax = ax.matshow(cm)
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

稍后使用新cm

的第四个子图
ax = fig.add_subplot(522,title='BNB')
cax = ax.matshow(cm)
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

带有新cm的最后一个子图

ax = fig.add_subplot(532,title='NC')
cax = ax.matshow(cm)
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)
pl.show()

我明白了:

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:5)

每次都更改子图的布局。您使用的fig.add_subplot(511)fig.add_subplot(n_rows, n_columns, index)的缩写。 n_rowsn_columns确定图中子图的布局,index位置(从1开始)。

因此,如果您想要有五行和两列,请进行

ax = fig.add_subplot(5,2,1)
(...)
ax = fig.add_subplot(5,2,2)
(...)    
ax = fig.add_subplot(5,2,3)

绘制第1行,第1列;第1列,第2列;第2行,第1列等。

再次注意fig.add_subplot(5,2,1)fig.add_subplot(521)是等效的。

相关问题