为啥当我做子图时窗口正在切割我的桌子?

Posted

技术标签:

【中文标题】为啥当我做子图时窗口正在切割我的桌子?【英文标题】:why the window is cutting my table when i am doing a subplot?为什么当我做子图时窗口正在切割我的桌子? 【发布时间】:2020-04-20 13:23:19 【问题描述】:

您好,我的目标是制作一个图表,在左侧有一个小表格,其中包含有关该图表的一些参数。

我的代码是这样的:

# Fazer o grafico
    table_data= [ ['δ', str(round(self.novodelta,3))],
    ['k', str(round(k,3))],
    ['h', str(round(h,3))],   ]

    plt.subplot(2, 1, 1)
    plt.plot(self.Observacoes,T, '-o')
    plt.plot(self.Observacoes,C, '-o')
    plt.plot(self.Observacoes,Lim_inf_CUSUM)
    plt.plot(self.Observacoes,Lim_sup_CUSUM)
    the_table = plt.table(cellText=table_data,
                  loc="left")
    plt.title("CUSUM")
    plt.xlabel("Amostras")
    plt.legend(["T", "C", "Limite inferiror", "Limite Superior"],loc='upper center', bbox_to_anchor=(0.5, -0.12),fancybox=True, shadow=True, ncol=4)
    plt.xticks(np.arange(min(self.Observacoes), max(self.Observacoes)+1, 1.0))
    plt.tight_layout()


    observacoes=self.Observacoes
    plt.subplot(2, 1, 2)
    plt.plot(observacoes,E, '-o')
    plt.plot(observacoes,LC_EWMA)
    plt.plot(observacoes,LSC_EWMA)
    plt.plot(observacoes,LIC_EWMA)
    plt.title("EWMA")
    plt.xlabel("Amostras")
    plt.legend(["E", "LC", "Limite inferiror", "Limite Superior"],loc='upper center', bbox_to_anchor=(0.5, -0.15),fancybox=True, shadow=True, ncol=4)
    plt.xticks(np.arange(min(self.Observacoes), max(self.Observacoes)+1, 1.0))
    plt.tight_layout()

您如何看到表格位于 y 轴顶部并且窗口正在切割第一列,我想解决这个问题,我的意思是这应该在表格之前和表格之后有一些空间,第二个图表将有另一张桌子,但我想先做这个

编辑

我忘了提到我正在使用 pyqt5 来构建这个,并且当我按下一个按钮弹出图表时这个附加

编辑 2

这是一个最小的、可重现的例子

import os
import sys
import matplotlib.pyplot as plt
from PyQt5 import uic, QtWidgets
import numpy as np
from matplotlib.gridspec import GridSpec
import statistics
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton



ui_path = os.path.dirname(os.path.abspath(__file__))
Ui_MainWindow, QtBaseClass = uic.loadUiType(os.path.join(ui_path, "problem.ui"))

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        button = QPushButton('PyQt5 button', self)

        button.clicked.connect(self.graph)

    def graph(self):
        self.values=[2,4,5,6]

        table_data= [ ['δ', 1],
    ['k', 2],
    ['h', 3],   ]

        plt.subplot(2, 1, 1)
        plt.plot(self.values,"-o")
        the_table = plt.table(cellText=table_data,
                  loc="left")
        plt.title("CUSUM")
        plt.xlabel("samples")
        plt.show()




if __name__ == "__main__":
    app =  QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

在这里你可以看到同样的问题:

enter image description here

【问题讨论】:

请提供minimal, reproducible example。 这就是你想要的吗? 【参考方案1】:

我找到了一个解决方案,但我不知道是否是最好的。

在最小的、可复现的例子的代码中

我创建了一个带有一行和两列的子图 我的想法是在第一列中绘制表格,在第二列中绘制图表

    plt.subplot(1, 2, 2)
    plt.plot(self.values,"-o")
    plt.title("CUSUM")
    plt.xlabel("samples")
    plt.subplot(1, 2, 1)
    the_table = plt.table(cellText=table_data,
                  loc="center") # tu put in the center 
    plt.axis('off') # turn off the axis and the outside box
    plt.show()

输出是:

enter image description here

【讨论】:

以上是关于为啥当我做子图时窗口正在切割我的桌子?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Fancybox 窗口会立即打开和关闭?

为啥这个窗口功能不起作用?我无法使用群组

三张桌子在同一个窗口,iPad

当我在 PyQt5 窗口中嵌入 Matplotlib 图形时,为啥有两个重复的轴标签?

尝试使用 .NET Core GraphQL 联合子图时,Apollo 联合网关返回错误

当我以编程方式从情节提要加载窗口时,为啥在 NSView 中未调用 drawRect?