2017-05-11 7 views
0

QTウィンドウで「graphを生成」ボタンをクリックすると、matplotlibを使ってグラフをプロットしようとしています。最初は問題が見つかりました.QTウィンドウを開いたときにプロットを閉じたり、制御したりすることはできません。しかし、私はこの解決策を見つけました: Cannot move Matplotlib plot window and exit it using red X button そして私はそれを空のプロットでテストしています。Python:QWidget:QPaintDeviceの前にQApplicationを構築する必要があります

のQWidget::私は私のコードを配置する場合しかし、私はこのエラーを取得する私のQTウィンドウでQPaintDevice前

をはQApplicationを作成する必要があり、私は置く:私のスクリプトmygraph.pyで

Process = subprocess.Popen(['python', 'mygraph.py'], shell=True).communicate() 

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

関連する問題