Change combobox option to modificate graphic/chart

自闭症网瘾萝莉.ら 提交于 2021-02-17 05:16:21

问题


I'm using PyQt5 to build up a user interface to show up data from "curve_fit" method.

As the data which I'm working can have discontinuities, my return from the method is divided on curve slices.

My GUI is composed by two graphs and one combo box. The function of the combo box is to enable the user to choose which slice the chart will show. Although the event of the combo box is working, my charts still the same, without any modification.

main.py

import sys
from PyQt5 import QtWidgets
import numpy as np

from mainwindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self,
                 initial_curve_x_value, initial_curve_y_value,
                 fit_curve_x_value_list, fit_curve_y_value_list,
                 fit_curve_error_value_list, fit_curve_zero_list,
                 fit_parameter_list, fit_square_R_list,
                 parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        self.initial_curve_x_value = initial_curve_x_value
        self.initial_curve_y_value = initial_curve_y_value
        self.fit_curve_x_value_list = fit_curve_x_value_list
        self.fit_curve_y_value_list = fit_curve_y_value_list
        self.fit_curve_error_value_list = fit_curve_error_value_list
        self.fit_curve_zero_list = fit_curve_zero_list
        self.fit_parameter_list = fit_parameter_list
        self.fit_square_R_list = fit_square_R_list

        rangecombobox = len(fit_square_R_list)
        itenscombobox = ["Slice " + str(i+1) for i in range(rangecombobox)]
        self.combobox.addItems(itenscombobox)
        #setting as initial value in chart as the first slice
        initial = 0
        x_test = self.initial_curve_x_value[initial]
        y_test = self.initial_curve_y_value[initial]
        x2_test = self.fit_curve_x_value_list[initial]
        y2_test = self.fit_curve_y_value_list[initial]
        error_y = self.fit_curve_error_value_list[initial]
        zero_y = self.fit_curve_zero_list[initial]

        self.static_ax1.plot(x_test, y_test)
        self.static_ax1.plot(x2_test, y2_test)

        self.static_ax2.plot(x2_test, error_y)
        self.static_ax2.plot(x2_test, zero_y)

        self.combobox.currentIndexChanged.connect(self.changePlot)

    def changePlot(self):
        combobox_index = self.combobox.currentIndex()

        self.static_ax1.clear()
        self.static_ax2.clear()

        #updating the values
        x_test = self.initial_curve_x_value[combobox_index]
        y_test = self.initial_curve_y_value[combobox_index]
        x2_test = self.fit_curve_x_value_list[combobox_index]
        y2_test = self.fit_curve_y_value_list[combobox_index]
        error_y = self.fit_curve_error_value_list[combobox_index]
        zero_y = self.fit_curve_zero_list[combobox_index]

        #updating the charts
        self.static_ax1.plot(x_test, y_test)
        self.static_ax1.plot(x2_test, y2_test)

        self.static_ax2.plot(x2_test, error_y)
        self.static_ax2.plot(x2_test, zero_y)

x1_1 = [i for i in range(10)]
x1_1 = np.array(x1_1)
x1_2 = [i for i in range(10, 20,1)]
x1_2 = np.array(x1_2)


y1_1 = [i for i in range(10, 20,1)]
y1_1 = np.array(y1_1)
y1_2 = [i for i in range(20, 30,1)]
y1_2 = np.array(y1_2)
y1_3 = [i for i in range(30, 40,1)]
y1_3 = np.array(y1_3)
y1_4 = [i for i in range(40, 50,1)]
y1_4 = np.array(y1_4)
y1_5 = [i for i in range(20, 30,1)]
y1_5 = np.array(y1_5)
y1_6 = [i for i in range(30, 40,1)]
y1_6 = np.array(y1_6)

initial_curve_x_value = []
initial_curve_x_value.append(x1_1)
initial_curve_x_value.append(x1_2)

initial_curve_y_value = []
initial_curve_y_value.append(y1_1)
initial_curve_y_value.append(y1_2)

fit_curve_x_value_list = []
fit_curve_x_value_list.append(x1_1)
fit_curve_x_value_list.append(x1_2)

fit_curve_y_value_list = []
fit_curve_y_value_list.append(y1_3)
fit_curve_y_value_list.append(y1_4)

fit_curve_error_value_list = []
fit_curve_error_value_list.append(y1_5)
fit_curve_error_value_list.append(y1_6)

fit_curve_zero_list = []
zero = np.zeros(10)
fit_curve_zero_list.append(zero)
fit_curve_zero_list.append(zero)

fit_parameter_list = [[1, 2, 4], [2,3]]
fit_square_R_list = [0.99, 0.98]

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mw = MainWindow(initial_curve_x_value, initial_curve_y_value,
                    fit_curve_x_value_list, fit_curve_y_value_list,
                    fit_curve_error_value_list, fit_curve_zero_list,
                    fit_parameter_list, fit_square_R_list
                    )
    mw.show()
    sys.exit(app.exec_())

mainwindow.py

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QLabel, QComboBox
from PyQt5.QtCore import QRect

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        length_window = 1200
        height_window = 1200
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(length_window, height_window)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.combobox = QComboBox(MainWindow)
        self.combobox.setGeometry(QRect(900, 80, 250, 40))

        self.chart1 = QtWidgets.QWidget(MainWindow)
        self.chart1.setGeometry(QRect(50, 50, 800, 400))
        self.layout_chart1 = QtWidgets.QVBoxLayout(self.chart1)
        self.static_canvas1 = FigureCanvasQTAgg(Figure(figsize=(8,4)))
        self.layout_chart1.addWidget(self.static_canvas1)

        self.chart2 = QtWidgets.QWidget(MainWindow)
        self.chart2.setGeometry(QRect(50, 450, 800, 400))
        self.layout_chart2 = QtWidgets.QVBoxLayout(self.chart2)
        self.static_canvas2 = FigureCanvasQTAgg(Figure(figsize=(8,4)))
        self.layout_chart2.addWidget(self.static_canvas2)

        self.static_ax1 = self.static_canvas1.figure.subplots()
        self.static_ax2 = self.static_canvas2.figure.subplots()


回答1:


You have to use the draw() method of the canvas to update the painting:

def changePlot(self):
    # ...

    self.static_ax2.plot(x2_test, error_y)
    self.static_ax2.plot(x2_test, zero_y)

    self.static_canvas1.draw()
    self.static_canvas2.draw()


来源:https://stackoverflow.com/questions/63103061/change-combobox-option-to-modificate-graphic-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!