简体   繁体   English

X轴在matplotlib中为固定大小(无法通过set_xlim更改)

[英]X-axis is fixed size in matplotlib (unable to change with set_xlim)

I'd like to remove part of the white part of the x-axis in the Hinton diagram that I'm plotting (see below). 我想删除正在绘制的欣顿图中x轴的白色部分(请参见下文)。 I've tried using ax1.set_xlim(1.5, width-4) , but that just moves the white space to the left side of the data. 我尝试使用ax1.set_xlim(1.5, width-4) ,但这只是将空白移到数据的左侧。 It would be easy to just fill up the white space with grey, but I'm not interested in doing that. 仅用灰色填充空白会很容易,但是我对此并不感兴趣。

Even if I change the yaxis by using ax1.set_ylim(0, height) , the x-axis changes.. 即使我使用ax1.set_ylim(0, height)更改y轴,x轴也会更改。

在此处输入图片说明

def _blob(x,y,area,colour):
    """
    Draws a square-shaped blob with the given area (< 1) at
    the given coordinates.
    """
    hs = np.sqrt(area) / 2
    xcorners = np.array([x - hs, x + hs, x + hs, x - hs])
    ycorners = np.array([y - hs, y - hs, y + hs, y + hs])
    P.fill(xcorners, ycorners, colour, edgecolor=colour)

def hinton(W, varLabels, maxWeight=None):
    """
    Draws a Hinton diagram for visualizing a weight matrix. 
    Temporarily disables matplotlib interactive mode if it is on, 
    otherwise this takes forever.
    """
    reenable = False
    if P.isinteractive():
        P.ioff()
    P.clf()
    height, width = W.shape
    if not maxWeight:
        maxWeight = 2**np.ceil(np.log(np.max(np.abs(W)))/np.log(2))

    fig  = plt.figure()  
    ax1  = fig.add_subplot(111) 
    ax1.set_xticks(np.arange(0,12))
    ax1.set_yticks(np.arange(0,12))
    ax1.tick_params(direction='inout')
    ax1.set_xticklabels(varLabels, rotation=45, ha='right')  
    ax1.set_yticklabels(["" for x in range(12)])    

    P.fill(np.array([0-0.5,width-0.5,width-0.5,0-0.5]),np.array([0-0.5,0-0.5,height,height]),'gray')
    #P.axis('off')
    P.axis('equal')
    for x in xrange(width):
        for y in xrange(height):
            _x = x+1
            _y = y+1
            w = W[y,x]
            if w > 0:
                _blob(_x - 1, height - _y + 0.5, min(1,w/maxWeight),'white')
            elif w < 0:
                _blob(_x - 1, height - _y + 0.5, min(1,-w/maxWeight),'black')
    ax1.set_xlim(1.5, width-1)
    if reenable:
        P.ion()
    P.show()


# Run the plot function
varLabels = ['n_contacts', 'n_calls', 'n_texts', 'dur_calls', 'morning', 'work-hours', 'evening', 'night', 'weekdays', 'friday', 'saturday', 'sunday']
hinton(conv_weights, varLabels)

Put

ax1.set_xlim(-0.5, width-0.5)
ax1.set_aspect('equal','box')

instead of 代替

ax1.set_xlim(1.5, width-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM