如何在PyQT for Regression中应用'scipy'和'pylab'库

时间:2017-04-28 16:30:46

标签: python pyqt5

嗯,在文字版中,我很容易得到答案,

实施例)

from scipy import stats,polyval
from pylab import plot,title,show,legend
import pylab
x=[3,2,3,4,5]
y=[2,3,4,5,6]
slope,intercept,r_value,p_value,stderr = stats.linregress(x,y)
ry=polyval([slope,intercept],x)
print(slope)
plot(x,y,'k.')
plot(x,ry,'r.-')

但是在PyQt中,当我写下这样的代码时,

def __init__(self):
    super().__init__()
    self.setupUI()

def setupUI(self):
    self.setGeometry(600, 200, 1200, 1000)
    self.setWindowTitle("PyChart Viewer v0.1")
    self.setWindowIcon(QIcon('icon.png'))

    self.lineEdit = QLineEdit()
    self.pushButton = QPushButton("DrawChart")
    self.pushButton.clicked.connect(self.pushButtonClicked)

    self.fig = plt.Figure() 
    self.canvas = FigureCanvas(self.fig)
    self.Rogistec=pl.Figure()
    self.RogistecCanvas=FigureCanvas(self.Rogistec)
    leftLayout = QVBoxLayout()
    leftLayout.addWidget(self.canvas)

    # Right Layout
    rightLayout = QVBoxLayout()
    rightLayout.addWidget(self.lineEdit)
    rightLayout.addWidget(self.pushButton)
    rightLayout.addWidget(self.RogistecCanvas)
    rightLayout.addStretch(1)

    layout = QHBoxLayout()
    layout.addLayout(leftLayout)
    layout.addLayout(rightLayout)
    layout.setStretchFactor(leftLayout, 1)
    layout.setStretchFactor(rightLayout, 0)

    self.setLayout(layout)

def pushButtonClicked(self):
    self.fig.clear()
    self.Rogistec.clear()
    code = self.lineEdit.text() 
    ax = self.fig.add_subplot(111)
    rogi=self.Rogistec.add_subplot(111)

    slope,intercept,r_value,p_value,stderr=stats.linregress(IntroUI.g_missingpeoplecnt,IntroUI.g_sortArrayDeadcnt) 


    if code=="":
        ax.plot(range(17), IntroUI.g_sortArrayDeadcnt, label='DeadPopulation') 
        ax.plot(range(17), IntroUI.g_victimcnt, label='VictimPeopleCnt')
        ax.plot(range(17), IntroUI.g_missingpeoplecnt, label='MissingPeopleCnt')
        ax.set_xticks(range(17))       
        ax.set_xticklabels(IntroUI.g_sortArrayDate,rotation=45,fontsize=7)
    ax.legend(loc='upper right') 
    ax.grid() 
    self.canvas.draw()
    self.RogistecCanvas.draw()

它不起作用。有什么问题?我想,我已尽我所能,但无法解决这个问题。等待你的帮助。

1 个答案:

答案 0 :(得分:0)

自我回答,这是因为我没有将类型字符串更改为int