python - PyQt5 Matplotlib figure- update with user input -
i new gui devolopment , trying develop app using pyqt5 in python. want show figure , few user widgets update figure through user input. below code:
import sys pyqt5.qtwidgets import qapplication, qmainwindow, qsizepolicy, qwidget, qcombobox, qlabel, qradiobutton, qcheckbox, qgridlayout, qlineedit matplotlib.backends.backend_qt5agg import figurecanvasqtagg figurecanvas matplotlib.figure import figure import matplotlib mpl import matplotlib.pyplot plt import random import numpy np class app(qwidget): def __init__(self): super().__init__() self.left = 0 self.top = 800 self.title = 'chip2 torque data' self.width = 800 self.height =800 self.initui() def initui(self): self.setwindowtitle(self.title) self.setgeometry(self.left, self.top, self.width, self.height) l = qgridlayout(self) #hbox = mymplcanvas(self.main_widget, width=50, height=40, dpi=100) self.figure = plt.figure(figsize=(15,5)) self.canvas = figurecanvas(self.figure) l.addwidget(self.canvas, 0,0,9,(100-4)) xselect=qradiobutton("x",self) xselect.setchecked(true) zselect=qradiobutton("z",self) sselect=qradiobutton("sp1",self) l.addwidget(xselect,0,(100-1)) l.addwidget(zselect,0,(100-2)) l.addwidget(sselect,0,(100-3)) pass_list=qcombobox(self) pass_list.additems(sheets_idealcut) l.addwidget(pass_list,1,(100-3),1,3) rawdata_check=qcheckbox("raw data",self) rawdata_check.setchecked(true) l.addwidget(rawdata_check,2,(100-3),1,3) mvgavg_check=qcheckbox("moving average",self) mvgavg_check.setchecked(true) mvgstd_check=qcheckbox("moving stdev",self) mvgstd_check.setchecked(true) l.addwidget(mvgavg_check,3,(100-3),1,3) l.addwidget(mvgstd_check,4,(100-3),1,3) xlim_left=qlineedit("none",self) xlim_right=qlineedit("none",self) ylim_top=qlineedit("none",self) ylim_top.textchanged.connect(self.plot) ylim_bottom=qlineedit("none",self) ylim_bottom.textchanged.connect(self.plot) xlim_left_label=qlabel("x min",self) xlim_right_label=qlabel("x max",self) ylim_top_label=qlabel("y max",self) ylim_bottom_label=qlabel("y min",self) l.addwidget(xlim_right_label,5,(100-3),1,1) l.addwidget(xlim_right,5,(100-2),1,2) l.addwidget(xlim_left_label,6,(100-3),1,1) l.addwidget(xlim_left,6,(100-2),1,2) l.addwidget(ylim_top_label,7,(100-3),1,1) l.addwidget(ylim_top,7,(100-2),1,2) l.addwidget(ylim_bottom_label,8,(100-3),1,1) l.addwidget(ylim_bottom,8,(100-2),1,2) self.compute_initial_figure() self.show() def compute_initial_figure(self): axes=self.figure.add_subplot(111) t = np.arange(0.0, 3.0, 0.01) s = np.sin(2*np.pi*t) axes.plot(t, s) self.canvas.draw() #axes.set_ylim(top=self.ylim_top.text(),bottom=self.ylim_bottom.text()) def plot(self): plt.cla() axes=self.figure.add_subplot(111) t = np.arange(0.0, 3.0, 0.01) s = np.sin(2*np.pi*t) axes.plot(t, s) axes.set_ylim(top=self.ylim_top.text(),bottom=self.ylim_bottom.text()) self.canvas.draw() if __name__ == '__main__': sheets_idealcut=['pass2','pass3','pass4','pass5'] app = qapplication(sys.argv) w = app() app.exec_()
i want change y axis limits in figure based on user input in y max
, y min
textboxes. below output:
problem is: figure not updating when change values in y max
, y min
textboxes. used qlineedit.textchanged.connect
function connect textbox figure update function. seems code not entering plot
function @ all. doing wrong here ?
please help. (1.i use grid layout can adjust widget size , positions desired. 2. this reference code used base code on. reference uses pyqt4.)
i expect code throws error, because trying set ylimits strings.
the ylimits should numerical values, 1 need convert texts input fields numbers.
axes.set_ylim(top=float(self.ylim_top.text()),bottom=float(self.ylim_bottom.text()))
apart may not idea clear axes , add new subplot each time values in text inputs changed. instead may use same axes (call self.axes
) operate on.
Comments
Post a Comment