更改组合框选项以修改图形/图表
Posted
技术标签:
【中文标题】更改组合框选项以修改图形/图表【英文标题】:Change combobox option to modificate graphic/chart 【发布时间】:2020-07-26 16:51:26 【问题描述】:我正在使用 PyQt5 构建用户界面以显示来自“curve_fit”方法的数据。
由于我正在处理的数据可能存在不连续性,因此我从该方法中返回的数据被划分为曲线切片。
我的 GUI 由两个图形和一个组合框组成。组合框的作用是使用户能够选择图表将显示哪个切片。 虽然组合框的事件有效,但我的图表还是一样,没有任何修改。
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_())
主窗口.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】:你必须使用画布的draw()
方法来更新绘画:
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()
【讨论】:
我之前尝试过这种方法,但使用的是“static_ax'i'”。现在它真的起作用了,谢谢你:)以上是关于更改组合框选项以修改图形/图表的主要内容,如果未能解决你的问题,请参考以下文章
在现有主窗口上显示在 PyQt GUI 上选择组合框选项的图表
如何更改代码以使组合框显示 displayField 值列表
如果用户需要向现有选项添加新选项,如何在访问中创建一个组合框,该组合框可以转换为文本框以启用书写?