我已经通过Joe Kington更改了代码并编写了以下内容:
import matplotlib.pyplot as plt
import numpy as np
import pandas
from matplotlib.table import Table
def main():
MY_List=[[1,2,5,56,6,7,7,7,7,5],[3,4,5,5,5,5,5,5,5,8], [4,5,6,6,7,7,4,3,4,2],[4,7,2,3,4,5,6,78,8,9]]
data = pandas.DataFrame(MY_List)
checkerboard_table(data)
plt.show()
def checkerboard_table(data, fmt='{:.2f}', bkg_colors=['C', 'white']):
fig, ax = plt.subplots()
ax.set_axis_off()
tb = Table(ax, bbox=[0,0,1,1])
nrows, ncols = data.shape
width, height = 1.0 / ncols, 1.0 / nrows
print(data)
# Add cells
for (i,j), val in np.ndenumerate(data):
# Index either the first or second item of bkg_colors based on
# a checker board pattern
idx = [j % 2, (j + 1) % 2][i % 2]
color = bkg_colors[idx]
#print(i,j,val)
#print(data)
tb.add_cell(i, j, width, height, text=fmt.format(val),
loc='center', facecolor=color)
# Row Labels...
for i, label in enumerate(data.index):
tb.add_cell(i, -1, width, height, text=label, loc='right',
edgecolor='none', facecolor='none')
# Column Labels...
for j, label in enumerate(data.columns):
tb.add_cell(-1, j, width, height/2, text=label, loc='center',
edgecolor='none', facecolor='none')
ax.add_table(tb)
plt.savefig("hk.png")
return fig
if __name__ == '__main__':
main()
这段代码给了我一个包含MY_list矩阵的表作为。我需要使用“for循环”更改MY_list矩阵,以使矩阵具有不同的大小(一些嵌套列表包含超过3000 * 2000个项目)。这个巨大的矩阵使得我的桌子细胞非常小,所以我看不到细胞内的任何数字,甚至在某些情况下,细胞很小,几乎看不到!
我需要根据矩阵的大小自动增加表格的大小而不改变每个单元格的大小。例如,我需要每个单元格具有固定的大小,并且在增加矩阵大小之后,此单元格大小不会变得更小。相反,我希望我的情节变得越来越大,如果我的矩阵越来越大(但不会减小每个细胞的大小,所以在细胞内部看不到任何东西!)。如果矩阵增加了大小,我需要通过在其自身的右侧和底部添加滚动条来增加它(就像我们在excel中看到的那样,因此我们可以在不改变单元大小的情况下添加大量数据)。
有没有人可以帮我解决这个问题?谢谢
通过ImportanceOfBeingErnest的评论帮助,我能够解决问题。我决定与其他人分享我的代码:
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
class ScrollableWindow(QtWidgets.QMainWindow):
def __init__(self, fig):
self.qapp = QtWidgets.QApplication([])
QtWidgets.QMainWindow.__init__(self)
self.widget = QtWidgets.QWidget()
self.setCentralWidget(self.widget)
self.widget.setLayout(QtWidgets.QVBoxLayout())
self.widget.layout().setContentsMargins(0,0,0,0)
self.widget.layout().setSpacing(0)
self.fig = fig
self.canvas = FigureCanvas(self.fig)
self.canvas.draw()
self.scroll = QtWidgets.QScrollArea(self.widget)
self.scroll.setWidget(self.canvas)
self.nav = NavigationToolbar(self.canvas, self.widget)
self.widget.layout().addWidget(self.nav)
self.widget.layout().addWidget(self.scroll)
self.show()
exit(self.qapp.exec_())
import matplotlib.pyplot as plt
import numpy as np
import pandas
from matplotlib.table import Table
def main():
MY_List=[[1,2,5,56,6,7,7,7,7,5],[3,4,5,5,5,5,5,5,5,8],[4,5,6,6,7,7,4,3,4,2],[4,7,2,3,4,5,6,78,8,9],[3,4,5,5,6,6,6,7,77,7],[1,2,3,3,4,5,6,6,7,7],
[3,4,4,5,5,5,4,4,4,4],[2,2,4,4,5,5,5,5,5,5],[2,2,3,3,4,4,3,2,3,3],[3,3,3,4,5,5,6,7,8,9],[1,1,2,3,4,5,6,6,7,8],[3,4,5,6,7,8,9,98,7,7]]
data = pandas.DataFrame(MY_List)
checkerboard_table(data)
plt.show()
def checkerboard_table(data, fmt='{:.2f}', bkg_colors=['C', 'white']):
MY_List=[[1,2,5,56,6,7,7,7,7,5],[3,4,5,5,5,5,5,5,5,8],[4,5,6,6,7,7,4,3,4,2],[4,7,2,3,4,5,6,78,8,9],[3,4,5,5,6,6,6,7,77,7],[1,2,3,3,4,5,6,6,7,7],
[3,4,4,5,5,5,4,4,4,4],[2,2,4,4,5,5,5,5,5,5],[2,2,3,3,4,4,3,2,3,3],[3,3,3,4,5,5,6,7,8,9],[1,1,2,3,4,5,6,6,7,8],[3,4,5,6,7,8,9,98,7,7]]
# create a figure and some subplots
fig, ax = plt.subplots(figsize=(len(MY_List[0])*0.5,len(MY_List)*0.5))
# pass the figure to the custom window
ax.set_axis_off()
tb = Table(ax, bbox=[0,0,1,1])
nrows, ncols = data.shape
width, height = 1.0 / ncols, 1.0 / nrows
print(data)
# Add cells
for (i,j), val in np.ndenumerate(data):
# Index either the first or second item of bkg_colors based on
# a checker board pattern
idx = [j % 2, (j + 1) % 2][i % 2]
color = bkg_colors[idx]
#print(i,j,val)
#print(data)
tb.add_cell(i, j, width, height, text=fmt.format(val),
loc='center', facecolor=color)
# Row Labels...
for i, label in enumerate(data.index):
tb.add_cell(i, -1, width, height, text=label, loc='right',
edgecolor='none', facecolor='none')
# Column Labels...
for j, label in enumerate(data.columns):
tb.add_cell(-1, j, width, height/2, text=label, loc='center',
edgecolor='none', facecolor='none')
ax.add_table(tb)
a = ScrollableWindow(fig)
plt.savefig("hk.png")
return fig
if __name__ == '__main__':
main()