首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >X轴在matplotlib中是固定大小的(不能用set_xlim更改)

X轴在matplotlib中是固定大小的(不能用set_xlim更改)
EN

Stack Overflow用户
提问于 2015-03-25 15:28:31
回答 1查看 179关注 0票数 0

我想删除我正在绘制的Hinton图中x轴的部分白色部分(见下文)。我尝试过使用ax1.set_xlim(1.5, width-4),但这只是将空白移动到数据的左侧。用灰色填充空白是很容易的,但我对此不感兴趣。

即使我使用ax1.set_ylim(0, height)更改yaxis,x轴也会更改..。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-25 16:21:56

代码语言:javascript
复制
ax1.set_xlim(-0.5, width-0.5)
ax1.set_aspect('equal','box')

而不是

代码语言:javascript
复制
ax1.set_xlim(1.5, width-1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29259887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档