首页 > 解决方案 > 为什么当我做子图时窗口正在切割我的桌子?

问题描述

嗨,我的目标是有一个图表,在左侧有一个小表格,上面有一些关于该图表的参数。

我的代码是这样的:

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

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

在此处输入图像描述

标签: pythonmatplotlibpyqt5subplot

解决方案


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

在最小的、可重现的示例的代码中

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

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

输出是:

在此处输入图像描述


推荐阅读