I want to plot charts after clicking on rows. The signal is transmitted, but the chart isn't plotted. But if I call a plotting function (self.hist()
) from MyPlot.__init__
, the chart is plotted.
I used this as an example, but it didn't help. How to embed matplotib in pyqt - for Dummies
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import QCoreApplication, Qt
from PyQt4.QtGui import QListWidget, QListWidgetItem, QApplication, QMessageBox
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import sqlite3
matplotlib.style.use('ggplot')
import pandas as pd
import numpy as np
# my plotting functions are in this class
class Myplot(FigureCanvas):
def __init__(self, parent=None):
self.fig = Figure()
FigureCanvas.__init__(self, self.fig)
#self.hist()
#this and next functions should plot
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))
self.draw()
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)
self.draw()
#list with rows. I want to draw charts after clicking on them
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)
#this function sends signal to the plotting functions
def Clicked(self):
mplot=Myplot()
index=self.currentRow()
if index == 0:
print '0000'
mplot.compute_initial_figure()
if index == 1:
print '1111'
mplot.hist()
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
#menu
exitAction = QtGui.QAction(QtGui.QIcon('Stuff/exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
self.statusBar().showMessage('Ready')
self.showMaximized()
self.setMinimumSize(600, 400)
self.setWindowTitle('ESPA')
self.setWindowIcon(QtGui.QIcon('Stuff/icon.png.'))
#setup window widgets
self.main_widget= QtGui.QWidget(self)
#lines up widgets horizontally
layout = QtGui.QHBoxLayout(self.main_widget)
mp=Myplot(self.main_widget)
p=Panel(self.main_widget)
layout.addWidget(p)
layout.addWidget(mp)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())