Matlibplot表拉伸和移动 [英] Matlibplot table stretching and moving
问题描述
我正在尝试调整此表格,使其占据整个窗口,但是上下左右两个滑块的组合都不允许我这样做,它们所做的只是在平移网格时缩小网格.
Im trying to adjust this table so that it will take up the entire window, but no combination of the top, bottom left and right sliders will allow me to do it, all they do is shrink the grid while translating it.
有没有办法垂直拉伸呢?
Is there a way to stretch this vertically?
这是我的代码:
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()
推荐答案
您要在轴下方创建表.看来您想在轴内 内创建它,
You are creating the table below the axes. It seems you would rather like to create it within the axes,
table = ax.table(..., loc="center")
要调整垂直跨度,可以设置比例,例如在y方向上将尺寸增加一倍:
To then adjust the vertical span, you may set the scale, e.g. to double the size in y direction:
table.scale(1,2)
结果将看起来像
或者,相对于轴指定表格的边界框.要填充整个轴,
Alternatively, specify the bounding box of the table with respect to the axes. To fill the complete axes,
table = ax.table(..., bbox=[0,0,1,1])
然后调用 fig.tight_layout()
可以照常调整边距.
Then calling fig.tight_layout()
adjusts the margins as usual.
这篇关于Matlibplot表拉伸和移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!