【问题标题】:How to increase the size of a table in matplotlib (by adding scroll bars)如何在 matplotlib 中增加表格的大小(通过添加滚动条)
【发布时间】:2023-03-25 06:21:01
【问题描述】:

我已将代码更改为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 中看到的那样,我们可以在那里添加大量数据而不改变单元格大小)。

有没有人可以帮我解决这个问题?谢谢

【问题讨论】:

  • 作为第一个近似值,使图形大小与每个方向上的单元格数量成正比。例如。要显示 n x m 矩阵,请使用 figsize=(n*0.05,m*0.05)。当然这有点不准确,因为你需要考虑边界。完成此操作后,如果它引起问题,肯定可以帮助进行微调。至于滚动条,请参见例如this question。再次,用您遇到问题的代码更新您的问题,这样可以提供帮助。
  • 非常感谢您的帮助。问题解决了,然后我为其他人添加了代码

标签: python matplotlib plot scrollbar


【解决方案1】:

通过 ImportanceOfBeingErnest 在 cmets 中的帮助,我能够解决问题。我决定将我的代码分享给其他人:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多