【发布时间】:2015-07-10 21:51:46
【问题描述】:
我目前正在学习 Python 和 PyQt。我在理解某些东西时遇到了问题。我的程序使用欧拉方法计算牛顿冷却定律的解。目前所有参数都是不变的,但用户稍后可以更改它们。
我想这样做,在按下“Plot”按钮后,信号被发送到带有新数据(不是旧数据)的函数 Plot。因此,如果我更改任何参数,按“绘图”后的结果会有所不同。
例如,在 main.py 中,我在开始时计算数据并使用 Window 的构造函数解析它,因为函数“Plot”在 Window 类中。因此,在按下“Plot”后,将绘制传递给 Window 对象的数据。之后,我再次计算数据,但使用不同的参数。再次按“绘图”后,我想用这些数据制作一个新的绘图。怎么做?最好的方法是什么?这是我的代码。提前谢谢你
main.py:
from mechanical_system import Mechanical_system
from calculus import Calculus
from window import Window
from PyQt4 import QtGui
import sys
if __name__ == "__main__":
system1 = Mechanical_system(200, 300, 100, 10, 40, 50)
data = Calculus.euler(system1.ODE, 100, 0, 100, 2)
print system1.get_params()
app = QtGui.QApplication(sys.argv)
main = Window(data)
main.show()
data = Calculus.euler(system1.ODE, 200, 0, 50, 0.5)
sys.exit(app.exec_())
演算.py:
import matplotlib.pyplot as plt
import numpy as np
class Calculus:
def __init__(self):
print "object created"
@staticmethod
def euler(f,y0,a,b,h):
"""y0 - initial temp, a-time = 0 , b-end of time gap, h - step"""
t,y = a,y0
time = [t]
value = [y]
while t <= b:
print "%6.3f %6.3f" % (t,y)
t += h
y += h * f(t,y)
time.append(t)
value.append(y)
data = {'time' : time, 'value' : value}
return data
@staticmethod
def draw_chart(time, value):
"""time dicionary contains moments for which each value was calculated"""
plt.axhline(20, 0, 100, color='r')
plt.plot(time, value, 'bo')
plt.axis([0, 100, 0, 100])
plt.xlabel("czas")
plt.ylabel("temperatura")
plt.show()
mechanical_system.py
class Mechanical_system:
#public variable holding system's parameters
system_parameters = {}
#Constructor
def __init__(self, momentum1, momentum2, n1, n2, w1, w2):
Mechanical_system.system_parameters = {'momentum1': momentum1, 'momentum2': momentum2, 'N1': n1, 'N2' : n2, 'w1' : w1, 'w2' : w2};
def get_params(self):
"""returns a dictionary that contains all the system parameters"""
return Mechanical_system.system_parameters
def set_param(self, param_name, value):
"""
sets a new value for specified parameter
available parameters: momentum1, momentum2, N1, N2, w1, w2
"""
Mechanical_system.system_parameters[param_name] = value
def ODE(self, time, temp):
"""ODE - ordinary differential equation describing our system"""
return -0.07 * (temp - 20)
window.py
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
class Window(QtGui.QDialog):
def __init__(self, data, parent = None):
super(Window, 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)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QtGui.QPushButton('Plot')
self.button.clicked.connect(lambda: self.plot(data['time'], data['value']))
self.lineEdit = QtGui.QLineEdit()
self.lineEdit.resize(200, 30)
self.lineEdit.setInputMethodHints((QtCore.Qt.ImhFormattedNumbersOnly))
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
layout.addWidget(self.lineEdit)
self.setLayout(layout)
def plot(self, time, value):
"""time dicionary contains moments for which each value was calculated"""
# create an axis
ax = self.figure.add_subplot(111)
# discards the old graph
ax.hold(False)
plt.axhline(20, 0, 100, color='r')
plt.axis([0, 100, 0, 100])
plt.xlabel("czas")
plt.ylabel("temperatura")
# plot data
plt.plot(time, value, '*-')
# refresh canvas
self.canvas.draw()
【问题讨论】:
标签: python python-2.7 matplotlib plot pyqt4