如何在matplotlib中增加表的大小(通过添加滚动条)

2024-05-17 08:10:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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()

这段代码给我一个表,其中包含我的列表矩阵this table。我需要用一个“for循环”来改变我的列表矩阵,使之具有不同大小的矩阵(有些嵌套列表包含超过3000*2000项)。这种巨大的矩阵使我的表细胞非常小,所以我看不到细胞内的任何数字,甚至在某些情况下细胞是如此微小,几乎看不见!在

我需要根据矩阵的大小自动增加表格的大小,而不改变每个单元格的大小。例如,我需要每个单元格都有一个固定的大小,并且在增加矩阵大小之后,这个单元格的大小不会变小。相反,如果我的矩阵越来越大,我希望我的图变得越来越大(但不要减小每个单元的大小,这样单元内部就看不到任何东西!)。如果矩阵的大小在增加,我需要绘图通过在其右侧和底部添加滚动条来增加它(就像我们在excel中看到的那样,这样我们就可以在不改变单元格大小的情况下添加大量数据)。在

有谁能帮我解决这个问题吗?谢谢


Tags: importnoneaddfordatamaintableplt
1条回答
网友
1楼 · 发布于 2024-05-17 08:10:06

在重要人物贝因杰内斯特的帮助下,我终于解决了这个问题。我决定与他人分享我的代码:

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()

相关问题 更多 >