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ウィンドウのクラスです。
私はあなたの解像度を使用するが、私はそれを理解していません。 matplotlibの問題はどこから来ていますか?なぜそれがそれを解決しましたか? –
QTアプリケーションでプロットウィンドウを開いたときに問題が発生しました。プロットウィンドウは開いてブロックされています(何もできません)。そして、解決策は "plt.switch_backend( 'Qt4Agg')"を追加することでした。本当に解決策を説明することはできません... – Ammoun