Matlibplot表拉伸和移动

时间:2018-03-12 17:45:59

标签: python matplotlib figure stretch

我试图调整此表以便占用整个窗口,但顶部,左下和右下滑块的组合不允许我这样做,他们所做的只是在翻译时缩小网格。 / p>

有没有办法垂直拉伸?

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
from random import randint

def colour(value):
    r,g,b = 0,0,0
    if value < 1:
        r = 1
        g = 1*value
    if value > 1:
        r = 1 - 1*(value-1)
        g = 1
    if value == 1:
        r = 1
        g = 1
    if value > 2:
        b = 1
    colour  =  (r,g,b)

    return colour

ncols, nrows = 7,8
row= [[]]*nrows
for i in range(nrows):
    row[i] = i*3

print(row)
col = ['M', 'T', 'W', 'Th', 'F', 'Sat', 'Sun']
list = []
text = ['','','','','','','']

for a in range(nrows):
     list.append(text)


fig, ax = plt.subplots()
ax.axis('tight')
ax.axis('off')
image = ax.table(cellText=list, colLabels = col, rowLabels=row)
for j in range(ncols):
    for i in range(nrows):
        print(i,j)
        value = randint(0,200)
        value = value / 100       
        image._cells[(i+1,j)].set_facecolor(colour(value))

figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
fig.tight_layout()
plt.show()

enter image description here

2 个答案:

答案 0 :(得分:1)

您正在轴下创建表。看来你更愿意在

中创建它
table = ax.table(..., loc="center")

要调整垂直范围,您可以设置比例,例如在y方向上加倍:

table.scale(1,2)

结果看起来像

enter image description here

或者,指定表格相对于轴的边界框。填补整个轴,

table = ax.table(..., bbox=[0,0,1,1])

然后调用fig.tight_layout()照常调整边距。

enter image description here

答案 1 :(得分:0)

使用缩放工具,选择比其高的区域(例如,右下角的左上角)更改宽高比。您也可以调整figsize以获得更好的宽高比。