Advertisement
Guest User

Untitled

a guest
Sep 24th, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import sys
  2.  
  3. import numpy as np
  4. import matplotlib
  5. matplotlib.use('Qt4Agg')
  6. import matplotlib.style
  7. from matplotlib.backends.backend_qt4agg import (
  8.     Figure,
  9.     FigureCanvasQTAgg as FigureCanvas)
  10. from PyQt4 import QtGui
  11. from PyQt4.uic import loadUiType
  12.  
  13.  
  14. Ui_MainWindow, QMainWindow = loadUiType('dialog.ui')
  15.  
  16. matplotlib.style.use('ggplot')
  17.  
  18.  
  19. class Main(QMainWindow, Ui_MainWindow):
  20.     def __init__(self, ):
  21.         super(Main, self).__init__()
  22.         self.setupUi(self)
  23.  
  24.         self.layout_mpl = QtGui.QVBoxLayout()
  25.         self.widget_mpl.setLayout(self.layout_mpl)
  26.  
  27.         self.prepare_figure()
  28.  
  29.         # Assign callbacks
  30.         self.button_refresh.clicked.connect(self.update_figure)
  31.  
  32.     def prepare_figure(self):
  33.         self.fig = Figure()
  34.         self.axes = self.fig.add_subplot(111)
  35.  
  36.         self.axes.set_xlim((0, 6))
  37.         self.axes.set_xlabel('seq.index')
  38.         self.axes.set_ylim((0, 100))
  39.         self.axes.set_ylabel('observation')
  40.  
  41.         N = 5
  42.         self.graph = self.axes.scatter(
  43.             [] * N, [] * N, s=[np.pi*5**2] * N, color='seagreen', animated=True)
  44.  
  45.         self.canvas = FigureCanvas(self.fig)
  46.         # w = self.widget_mpl.width()
  47.         # h = self.widget_mpl.height()
  48.         # self.canvas.setFixedSize(w, h)
  49.  
  50.         self.canvas.draw()
  51.         self.layout_mpl.addWidget(self.canvas)
  52.  
  53.         self.bg = self.canvas.copy_from_bbox(self.axes.bbox)
  54.  
  55.     def update_figure(self):
  56.         self.canvas.restore_region(self.bg)
  57.  
  58.         # Set data to be visualized
  59.         self.graph.set_offsets([(i+1, d) for i, d in
  60.                                 enumerate(np.random.rand(5)*100)])
  61.  
  62.         # Redraw
  63.         self.axes.draw_artist(self.graph)
  64.         self.canvas.blit(self.axes.bbox)
  65.  
  66.  
  67. if __name__ == '__main__':
  68.     app = QtGui.QApplication(sys.argv)
  69.     main = Main()
  70.     main.show()
  71.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement