В pyqt построение различных графиков matplotlib после кликов по строкам?

Хочу строить разл. графики после кликов на соотв. строки в QListWidget. Сигнал через connect передается, но график не строится (ничего не происходит), причем если поместить в __init__ вызов функции графика (self.hist()), то график строится, а если пытаться построить его через сигнал, то нет :/

часть кода, связанная с этой проблемой
class Myplot(FigureCanvas):
    def __init__(self, parent=None):
        self.fig = Figure()
        FigureCanvas.__init__(self, self.fig)
#        self.hist()    
    def compute_initial_figure(self):
        self.axes = self.fig.add_subplot(1,1,1)
        self.axes.hold(False)
        self.axes.plot(np.random.rand(400))
    def hist(self):
        tm = pd.Series(np.random.randn(500), index=pd.date_range('1/1/2005', periods=500))
        self.axes = self.fig.add_subplot(1,1,1)
        self.axes.hold(False)
        tm = tm.cumsum()
        self.axes.plot(tm)
        
class Panel (QtGui.QListWidget):
    def __init__(self, parent=None):
        QtGui.QListWidget.__init__(self)
        self.row1 = "COMMISSIONS & FEES"
        self.addItem(self.row1)
        row2 = "NET LIQUIDATING VALUE"
        self.addItem(row2)
        self.setFixedWidth(200)
        self.itemClicked.connect(self.Clicked)
    def Clicked(self):
        mplot=Myplot()
        index=self.currentRow()
        if index == 0:
            print '0000'
            mplot.compute_initial_figure()
        if index == 1:
            mplot.hist()


Весь код pastebin.com/H54QsmQW

Также буду не против любой критики/советов
  • Вопрос задан
  • 2617 просмотров
Пригласить эксперта
Ответы на вопрос 1
@greowe Автор вопроса
Ничего больше не нашел, кроме как объединить оба класса в один, только так заработало
class Myplot(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Myplot, self).__init__(parent)
        # a figure instance to plot on
        self.figure = plt.figure()
        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)
        self.mylist=QListWidget(self)
        row1="COMMISSIONS & FEES"
        self.mylist.addItem(row1)
        row2="NET LIQUIDATING VALUE"
        self.mylist.addItem(row2)
        self.mylist.setFixedWidth(200)
        self.mylist.itemClicked.connect(self.Clicked)

        # set the layout
        minlayout = QtGui.QHBoxLayout()
        minlayout.addWidget(self.mylist)
        minlayout.addWidget(self.canvas)
        self.setLayout(minlayout)
#        self.hist()    
    def compute_initial_figure(self):
        # random data
        xdata = [random.random() for i in range(10)]
        # create an axis
        ax = self.figure.add_subplot(111)
        # discards the old graph
        ax.hold(False)
        # plot data
        ax.plot(xdata)
        # refresh canvas
        self.canvas.draw()
    def hist(self):
        tm = pd.Series(np.random.randn(500), index=pd.date_range('1/1/2005', periods=500))
        tm = tm.cumsum()
        ax = self.figure.add_subplot(111)
        ax.hold(False)
        ax.plot(tm)
        self.canvas.draw()
    
    def Clicked(self):
        index=self.mylist.currentRow()
        if index == 0:
            print '0000'
            self.compute_initial_figure()
        if index == 1:
            print '1111'
            self.hist()
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы