Advertisement
Guest User

pyqt test with matplotlib and 3D

a guest
Apr 20th, 2010
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from PyQt4.QtGui import *
  5. from PyQt4.QtCore import *
  6.  
  7. from numpy import *
  8. from scipy.integrate import odeint
  9. import mpl_toolkits.mplot3d as p3
  10. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  11. from matplotlib.figure import Figure
  12.  
  13. class MyForm(QDialog):
  14.     def __init__(self, parent = None):
  15.         super(MyForm, self).__init__(parent)
  16.         self.setWindowTitle('Lorenz System (matplotlib and PyQt) - yus')
  17.         self.resize(420, 400)
  18.         # widgets
  19.         self.plot = LorenzPlot()
  20.         self.r_label = QLabel(u"<font size=7 color=darkred> \u03C1 =</font>")   #rho symbol
  21.         self.s_label = QLabel(u"<font size=7 color=darkred> \u03C3 = 8.0</font>")   #sigma symbol
  22.         self.b_label = QLabel(u"<font size=7 color=darkred> \u03B2 = 8/3</font>")   #beta symbol
  23.         self.r_intput = QDoubleSpinBox()
  24.         self.r_intput.setDecimals(1)
  25.         self.r_intput.setRange(0.0, 50)
  26.         # layout
  27.         layout = QGridLayout()
  28.         layout.addWidget(self.plot, 0, 0, 10, 10)
  29.         layout.addWidget(self.r_label, 10, 0)
  30.         layout.addWidget(self.s_label, 10, 3)
  31.         layout.addWidget(self.b_label, 10, 6)
  32.         layout.addWidget(self.r_intput, 10, 1)        
  33.         self.setLayout(layout)
  34.         #signal
  35.         self.connect(self.r_intput, SIGNAL("valueChanged(double)"), self.r_adjust)        
  36.         self.r_intput.setValue(27)
  37.        
  38.     def r_adjust(self, r_new):
  39.         self.plot.draw_plot(r = r_new)
  40.  
  41. class LorenzPlot(QWidget):
  42.     def __init__(self, *args):
  43.         QWidget.__init__(self, *args)
  44.        
  45.         self.fig = Figure((5.0, 4.0)) # 5" by 4"
  46.         self.ax = p3.Axes3D(self.fig)
  47.         self.canvas = FigureCanvas(self.fig)
  48.         self.canvas.setParent(self)
  49.    
  50.     def resizeEvent(self, ev):
  51.         self.fig.set_size_inches(10,8)
  52.         self.ax = p3.Axes3D(self.fig)
  53.         self.canvas = FigureCanvas(self.fig)
  54.         self.canvas.setParent(self)
  55.         self.draw_plot()
  56.  
  57.     def Lorenz(self, w, t, s, r, b):
  58.         x, y, z = w
  59.         return array([s*(y-x), r*x-y-x*z, x*y-b*z])
  60.  
  61.     def draw_plot(self, s=8.0, r=28.1, b=8/3.0):
  62.         # Parameters
  63.         self.s, self.r, self.b = s, r, b
  64.        
  65.         self.w_0 = array([0., 0.8, 0.])         # initial condition
  66.         self.time = arange(0., 100., 0.01)      # time vector
  67.         #integrate a system of ordinary differential equations
  68.         self.trajectory = odeint(self.Lorenz, self.w_0, self.time, args=(self.s, self.r, self.b))
  69.        
  70.         self.x = self.trajectory[:, 0]
  71.         self.y = self.trajectory[:, 1]
  72.         self.z = self.trajectory[:, 2]
  73.        
  74.         self.ax = p3.Axes3D(self.fig)
  75.         self.ax.plot3D(self.x, self.y, self.z)
  76.         self.canvas.draw()
  77.  
  78. if __name__ == '__main__':
  79.     app = QApplication(sys.argv)
  80.     form = MyForm()
  81.     form.show()
  82.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement