Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python pyqt matplotlib计算GUI应用程序打印错误_Python_Matplotlib_Pyqt4 - Fatal编程技术网

python pyqt matplotlib计算GUI应用程序打印错误

python pyqt matplotlib计算GUI应用程序打印错误,python,matplotlib,pyqt4,Python,Matplotlib,Pyqt4,各位!! 我正在从事一个项目,使用python pyqt4设计一个GUI,用于计算光伏板的V-I曲线。我想输入所需参数,计算光伏板输出电流,并使用matplotlib绘制V-I曲线。 我的代码运行良好。然而,我发现我从应用程序得到的曲线是错误的,而直接计算它是正确的。计算方法完全相同。但当我把计算放在GUI中时,情况就不同了。 代码如下: import sys from PyQt4 import QtGui from matplotlib.backends.backend_qt4agg impo

各位!! 我正在从事一个项目,使用python pyqt4设计一个GUI,用于计算光伏板的V-I曲线。我想输入所需参数,计算光伏板输出电流,并使用matplotlib绘制V-I曲线。 我的代码运行良好。然而,我发现我从应用程序得到的曲线是错误的,而直接计算它是正确的。计算方法完全相同。但当我把计算放在GUI中时,情况就不同了。 代码如下:

import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import fsolve
q = 1.6 * (10 ** -19)
k = 1.38 * (10 ** -23)
Tref = 25
Eg = 1.1

def calReverseSaturationCurrent(Isc,Voc,Ns,A,Tc):
    a = np.exp(q*Voc/(Ns*k*A*Tc))-1
    return Isc/a
def calSaturationCurrent(Irs,Tc,A):
    return Irs*((Tc/Tref)**3)*np.exp(q*Eg*(1/Tref - 1/Tc)/(k*A))
def calPhotocurrent(Isc,Ki,Tc,insolation):
    return (Isc + Ki*(Tc-Tref))*insolation
def calOutputCurrent(Isc,Voc,Ns,A,Tc,Ki,insolation,Np,Rs,Rsh,V):
    Irs = calReverseSaturationCurrent(Isc,Voc,Ns,A,Tc)
    Is = calSaturationCurrent(Irs,Tc,A)
    Iph = calPhotocurrent(Isc,Ki,Tc,insolation)
    I0 = 0
    I = np.array([])
    def f(ii,*arg):
        temp_v = arg[0]
        return ii - Np*Iph+Np*Is*(np.exp(q*(temp_v/Ns+ii*Rs/Np)/(k*Tc*A))-1)
    for vv in V:
        ii=fsolve(f,I0,args=vv)
        I = np.append(I,ii)
    return I
class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()
    def initUI(self):

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)

        self.toolbar = NavigationToolbar(self.canvas, self)
        self.toolbar.hide()

        self.idealFactor = QtGui.QLabel('Ideal Factor A')
        self.shortCircuitCurrent = QtGui.QLabel('Short Circuit Current')
        self.openCircuitVoltage = QtGui.QLabel('Open Circuit Voltage')
        self.temperatureCoefficient = QtGui.QLabel('Temperature Coefficient')
        self.seriesCells = QtGui.QLabel('Series Cells')
        self.parallelCells = QtGui.QLabel('Parallel Cells')
        self.seriesResistance = QtGui.QLabel('Series Resistance')
        self.shuntResistance = QtGui.QLabel('Shunt Resistance')
        self.outputVoltageMin = QtGui.QLabel('Min Output Voltage')
        self.outputVoltageMax = QtGui.QLabel('Max Output Voltage')
        self.workingTemperature = QtGui.QLabel('Working Temperature')
        self.isolation = QtGui.QLabel('Isolation')

        self.idealFactorEdit = QtGui.QDoubleSpinBox()
        self.idealFactorEdit.setValue(1.0)
        self.shortCircuitCurrentEdit = QtGui.QDoubleSpinBox()
        self.shortCircuitCurrentEdit.setValue(3.17)
        self.openCircuitVoltageEdit = QtGui.QDoubleSpinBox()
        self.openCircuitVoltageEdit.setValue(21.8)
        self.temperatureCoefficientEdit = QtGui.QDoubleSpinBox()
        self.temperatureCoefficientEdit.setValue(0.065)
        self.seriesCellsEdit = QtGui.QDoubleSpinBox()
        self.seriesCellsEdit.setValue(36.0)
        self.parallelCellsEdit = QtGui.QDoubleSpinBox()
        self.parallelCellsEdit.setValue(1.0)
        self.seriesResistanceEdit = QtGui.QDoubleSpinBox()
        self.seriesResistanceEdit.setValue(0.1)
        self.shuntResistanceEdit = QtGui.QDoubleSpinBox()
        self.shuntResistanceEdit.setValue(float("inf"))
        self.outputVoltageMinEdit = QtGui.QDoubleSpinBox()
        self.outputVoltageMinEdit.setValue(8)
        self.outputVoltageMaxEdit = QtGui.QDoubleSpinBox()
        self.outputVoltageMaxEdit.setValue(11)
        self.workingTemperatureEdit = QtGui.QDoubleSpinBox()
        self.workingTemperatureEdit.setValue(25)
        self.isolationEdit = QtGui.QDoubleSpinBox()
        self.isolationEdit.setValue(1)

        self.calBtn = QtGui.QPushButton('Get V-I',self)
        self.calBtn.clicked.connect(self.getVI)

        grid = QtGui.QGridLayout()
        self.setLayout(grid)

        grid.addWidget(self.idealFactor, 1, 0)
        grid.addWidget(self.idealFactorEdit, 1, 1)

        grid.addWidget(self.shortCircuitCurrent, 1, 2)
        grid.addWidget(self.shortCircuitCurrentEdit, 1, 3)

        grid.addWidget(self.openCircuitVoltage, 2, 0)
        grid.addWidget(self.openCircuitVoltageEdit, 2, 1)

        grid.addWidget(self.temperatureCoefficient, 2, 2)
        grid.addWidget(self.temperatureCoefficientEdit, 2, 3)

        grid.addWidget(self.seriesCells, 3, 0)
        grid.addWidget(self.seriesCellsEdit, 3, 1)

        grid.addWidget(self.parallelCells, 3, 2)
        grid.addWidget(self.parallelCellsEdit, 3, 3)

        grid.addWidget(self.seriesResistance, 4, 0)
        grid.addWidget(self.seriesResistanceEdit, 4, 1)

        grid.addWidget(self.shuntResistance, 4, 2)
        grid.addWidget(self.shuntResistanceEdit, 4, 3)

        grid.addWidget(self.outputVoltageMax, 5, 0)
        grid.addWidget(self.outputVoltageMaxEdit, 5, 1)

        grid.addWidget(self.outputVoltageMin, 5, 2)
        grid.addWidget(self.outputVoltageMinEdit, 5, 3)

        grid.addWidget(self.workingTemperature, 6, 0)
        grid.addWidget(self.workingTemperatureEdit, 6, 1)

        grid.addWidget(self.isolation, 6, 2)
        grid.addWidget(self.isolationEdit, 6, 3)

        grid.addWidget(self.calBtn, 7,0)
        grid.addWidget(self.canvas, 8, 0, 6, 0)


        self.setGeometry(300, 300, 1000, 800)
        self.setWindowTitle('PYPV')
        self.setWindowIcon(QtGui.QIcon('pvPanel.png'))        

        self.show()

    def getVI(self):
        A = self.idealFactorEdit.value()
        Isc = self.shortCircuitCurrentEdit.value()
        Voc = self.openCircuitVoltageEdit.value()
        Ki = self.temperatureCoefficientEdit.value()
        Ns = self.seriesCellsEdit.value()
        Np = self.parallelCellsEdit.value()
        Rs = self.seriesResistanceEdit.value()
        Rsh = self.shuntResistanceEdit.value()
        Vmin = self.outputVoltageMinEdit.value()
        Vmax = self.outputVoltageMaxEdit.value()
        V = np.linspace(Vmin,Vmax,1000)
        Tc = self.workingTemperatureEdit.value()
        insolation = self.isolationEdit.value()
        I = calOutputCurrent(Isc,Voc,Ns,A,Tc,Ki,insolation,Np,Rs,Rsh,V)
        plt.cla()
        ax = self.figure.add_subplot(111)
        ax.plot(V,I)
        self.canvas.draw()


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()    
我确信计算方法是正确的。我在没有GUI的情况下进行了测试。结果很好。代码如下:

import numpy as np
from scipy.optimize import fsolve
from matplotlib import pyplot as plt

q = 1.6 * (10 ** -19)
k = 1.38 * (10 ** -23)
Tref = 25
Eg = 1.1
A = 1.0
V = np.linspace(8,11,1000)
Isc = 3.17
Ki = 0.07
Ns = 36.0
Np = 1.0
Voc = 21.8
Rs = 0.1
#Rsh = float("inf")
Tc = 25
insolation = 1

def calReverseSaturationCurrent(Isc,Voc,Ns,A,Tc):
    a = np.exp(q*Voc/(Ns*k*A*Tc))-1
    return Isc/a

def calSaturationCurrent(Irs,Tc,A):
    return Irs*((Tc/Tref)**3)*np.exp(q*Eg*(1/Tref - 1/Tc)/(k*A))

def calPhotocurrent(Isc,Ki,Tc,insolation):
    return (Isc + Ki*(Tc-Tref))*insolation

def calOutputCurrent(Isc,Voc,Ns,A,Tc,Ki,insolation,Np,Rs,Rsh,V):
    Irs = calReverseSaturationCurrent(Isc,Voc,Ns,A,Tc)
    Is = calSaturationCurrent(Irs,Tc,A)
    Iph = calPhotocurrent(Isc,Ki,Tc,insolation)
    I0 = 0
    I = np.array([])
    def f(ii,*arg):
        temp_v = arg[0]
        return ii - Np*Iph+Np*Is*(np.exp(q*(temp_v/Ns+ii*Rs/Np)/(k*Tc*A))-1)
    for vv in V:
        ii=fsolve(f,I0,args=vv)
        I = np.append(I,ii)
    return I

def makePlots(I,V):
    plt.plot(V,I)
    plt.show()

def test():
    I = calOutputCurrent(Isc,Voc,Ns,A,Tc,Ki,insolation,Np,Rs,Rsh,V)
    makePlots(I,V)

if __name__ == "__main__":
    test()
以下是两幅图:

英语不是我的第一语言。我希望我能很好地描述这个问题。
提前谢谢

我猜您使用的是Python-2,
/
运算符对整数的行为与对浮点的行为不同。在第一种情况下,它是一个整数除法:结果是一个整数,余数被丢弃。在第二种情况下,这是您通常期望从部门获得的结果。这是许多错误的根源,因此在Python-3中,
/
运算符始终是一个常规除法(返回浮点),整数除法可以用
/
完成。有关更多详细信息,请参阅。请注意,
calSaturationCurrent
函数包含这样一个整数除法:
1/Tc
在Python-2中产生0

要使Python-2的行为类似于Python 3,可以使用“未来导入”。如果在程序顶部(在其他导入语句之前)添加以下行,则无论是否使用GUI,都会得到相同的结果

from __future__ import division
现在,您可能也在非GUI版本中使用了Python-2,那么为什么首先会得到不同的结果呢?我最好的猜测是,在程序的gui版本中,从Qt获得的值略有不同(远远落后于小数点)。您的程序易受此影响这一事实表明它可能有bug


例如,如果打印
calSaturationCurrent
功能的结果,则不带GUI时为3.42723507299e-122,带GUI时为0.0。这意味着函数
f
(您试图求解其根)总是非常接近0或正好为0。我不相信结果,而是从绘制
f
本身开始。

我猜您使用的是Python-2,其中
/
运算符对整数的行为与对浮点的行为不同。在第一种情况下,它是一个整数除法:结果是一个整数,余数被丢弃。在第二种情况下,这是您通常期望从部门获得的结果。这是许多错误的根源,因此在Python-3中,
/
运算符始终是一个常规除法(返回浮点),整数除法可以用
/
完成。有关更多详细信息,请参阅。请注意,
calSaturationCurrent
函数包含这样一个整数除法:
1/Tc
在Python-2中产生0

要使Python-2的行为类似于Python 3,可以使用“未来导入”。如果在程序顶部(在其他导入语句之前)添加以下行,则无论是否使用GUI,都会得到相同的结果

from __future__ import division
现在,您可能也在非GUI版本中使用了Python-2,那么为什么首先会得到不同的结果呢?我最好的猜测是,在程序的gui版本中,从Qt获得的值略有不同(远远落后于小数点)。您的程序易受此影响这一事实表明它可能有bug


例如,如果打印
calSaturationCurrent
功能的结果,则不带GUI时为3.42723507299e-122,带GUI时为0.0。这意味着函数
f
(您试图求解其根)总是非常接近0或正好为0。我不相信结果,我会从绘制
f
本身开始。

非常感谢!在我更改Tc=25.0后,我得到了正确的结果!我从你的回答中学到了很多。很高兴能帮上忙。请将问题标记为“已回答”。非常感谢!在我更改Tc=25.0后,我得到了正确的结果!我从你的回答中学到了很多。很高兴能帮上忙。请将问题标记为“已回答”。