我发现了这个关于如何用matplotlib:http://www.scipy.org/Cookbook/Matplotlib/HintonDiagrams制作hinton图的示例。在
我需要多个图表和一个在记号上有标签的yaxis,所以我像这样重写它
import matplotlib.pyplot as plt
import numpy as N
def hintonForTwo(W1, W2, maxWeight=None):
height, width = W1.shape
if not maxWeight:
maxWeight = 2**N.ceil(N.log(N.max(N.abs(W1)))/N.log(2))
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, sharey=True)
hintonSubPlot(ax1, W1, maxWeight)
ax1.set_yticks(N.arange(height))
# the input matrices will have row height of 7
# and years 1981, 1986... up to 2011 are supposed to be the ytick labels
ax1.set_yticklabels(tuple(range(1981,2012,5)))
ax1.set_aspect('equal')
ax1.set_axis_off()
hintonSubPlot(ax2, W2, maxWeight)
return fig, ax1, ax2
def hintonSubPlot(F, W, maxWeight):
height, width = W.shape
F.fill(N.array([0,width,width,0]),N.array([0,0,height,height]),'white')
for x in xrange(width):
for y in xrange(height):
_x = x+1
_y = y+1
w = W[y,x]
if w > 0:
_blobAlt(F, _x - 0.5, height - _y + 0.5, min(1,w/maxWeight),'black')
elif w < 0:
_blobAlt(F, _x - 0.5, height - _y + 0.5, min(1,-w/maxWeight),'gray')
return F
def _blobAlt(F, x,y,area,colour):
hs = N.sqrt(area) / 2
xcorners = N.array([x - hs, x + hs, x + hs, x - hs])
ycorners = N.array([y - hs, y - hs, y + hs, y + hs])
F.fill(xcorners, ycorners, colour, edgecolor=colour)
dim = (7,50)
X1 = N.ones(dim) * N.nan
X2 = N.ones(dim) * N.nan
rm = N.random.random(dim)
X1[rm > 2.0/3] = 1
X1[rm < 1.0/3] = -1
rm = N.random.random(dim)
X2[rm > 2.0/3] = 1
X2[rm < 1.0/3] = -1
f, a, b = hintonForTwo(X1, X2)
f.savefig('tmp.png')
但有几个问题我无法解决:
那么,我怎样才能得到ytick标签,但没有大的白色长方形围绕子ig和没有大的差距之间的顶部和底部?在
你的代码仍然不完整。但是,如果您只想调整子批次之间的间距,有选择地删除一些级别,您可以尝试:
相关问题 更多 >
编程相关推荐