2017-05-11 36 views
0

我試圖在QT窗口中單擊名爲「生成圖形」的按鈕時使用matplotlib繪製圖形。起初,我發現了一個問題:當QT窗口打開時,我無法關閉繪圖或控制它。但我發現這個解決方案: Cannot move Matplotlib plot window and exit it using red X button 我測試它在一個空的情節,它的工作原理。然而,當我把我的代碼,我得到這個錯誤:Python:QWidget:必須在QPaintDevice之前構造一個QApplication

QWidget的:必須構造一個QApplication的一個的QPaintDevice前

在我的Qt窗口,我把:

Process = subprocess.Popen(['python', 'mygraph.py'], shell=True).communicate() 
在我的腳本mygraph.py

def main(): 


    print("Beginning plot for section 2.1 ...") 
    #Get the selected iteration and sector 
    iter = InterfaceVariationTRANUS("config").DropDownListDisplayIter2_1.currentIndex() 
    sector = InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentIndex() 
    #Access the corresponding IMPLOC file and extract the data for the selected sector 
    nameDirectory = InterfaceVariationTRANUS("config").nameDirectory2_1 + str(iter) 
    pathToDirectory = os.path.join(InterfaceVariationTRANUS("config").pathOutputDirectoryInstance, nameDirectory) 
    filepath = os.path.join(pathToDirectory, "IMPLOC_J.MTX") 
    matrix = pd.read_csv(filepath) 
    matrix.columns = ["Scen", "Sector", "Zone", "TotProd", "TotDem", "ProdCost", "Price", "MinRes", "MaxRes", "Adjust"] 
    #Removal of the noise (production equal to zero => adjust equal to zero) 
    #matrix.Adjust[matrix.TotProd == 0] = 0 
    row_index = matrix.TotProd == 0 
    matrix.loc[row_index,'Adjust'] = 0 

    #matrix.Adjust[matrix.Price == 0] = 0 
    row_index = matrix.Price == 0 
    matrix.loc[row_index,'Adjust'] =0 

    #matrix.Price[matrix.Price == 0] = None 
    row_index = matrix.Price == 0 
    matrix.loc[row_index,'Price'] = None 

    matrix2 = matrix[["Sector","Zone", "Price", "Adjust"]] 
    #Isolation of the data for the sector selected 
    nameSector = str(InterfaceVariationTRANUS("config").stockParam.list_sectors[sector])+" "+(InterfaceVariationTRANUS().stockParam.list_names_sectors[sector]) 
    matrix3 = matrix2[matrix2["Sector"].str.contains(nameSector) == True] 
    matrix4 = matrix3[matrix3["Zone"].str.contains("ext_") == False] 
    #This boolean is used to allow for multiple graphes to be shown on the same panel. 

    firstPlot = False 

    #Plot graph and display it 

    if(InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentIndex() != InterfaceVariationTRANUS("config").currentSectorPlot2_1): 
     InterfaceVariationTRANUS("config").numFiguresPlot2_1 = InterfaceVariationTRANUS("config").numFiguresPlot2_1 + 1 
     InterfaceVariationTRANUS("config").currentSectorPlot2_1 = InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentIndex() 
     firstPlot = True 

    fig = plt.figure(self.numFiguresPlot2_1) 
    fig.canvas.set_window_title(InterfaceVariationTRANUS("config").DropDownListDisplaySector2_1.currentText()) 
    x = np.arange(0, InterfaceVariationTRANUS("config").stockParam.nTotZones, 1) 
    y = pd.to_numeric(matrix4["Price"]) 
    print("Moyenne = ") 
    print(y.mean(0)) 

    z = pd.to_numeric(matrix4["Adjust"]*y)/100 + y 
    # plot data 
    if(firstPlot): 
     price = plt.plot(x, y, label = ("Price")) 
    shadowPrice = plt.plot(x, z, label = ("Price + adjust for iteration "+InterfaceVariationTRANUS("config").DropDownListDisplayIter2_1.currentText())) 

    plt.legend() 
    plt.show(block=False) #method 3 
    plt.draw() 

如果 == '主要':

main() 

其中InterfaceVariationTRANUS是我的QT窗口的類。

回答

0

最後,我發現,正常工作爲我的問題的解決方案:

import matplotlib.pyplot as plt 
plt.switch_backend('Qt4Agg') 
... 

plt.legend() 
plt.draw() 
plt.show(block=False) 
+0

我用您的決議,但我不明白它。 matplotlib的問題來自哪裏?爲什麼這樣解決了它? –

+1

問題是在QT應用程序中打開一個圖形窗口,圖形窗口打開並被阻塞(你無法對它做任何事情)。解決方案是添加「plt.switch_backend('Qt4Agg')」。真的無法解釋解決方案... – Ammoun

相關問題