0
votes

I am trying to plot graphs using matplotlib when clicked on a button called "generate graph" in a QT window. At first, I found a problem : I cannot close plots or control it when the QT window is opened. But I found this solution : Cannot move Matplotlib plot window and exit it using red X button and I test it on an empty plot and it works. However, when I put my code I get this error :

QWidget: Must construct a QApplication before a QPaintDevice

In my qt window I put :

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

in my script 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()

if name == 'main':

main()

Where InterfaceVariationTRANUS is the class of my QT window.

1

1 Answers

0
votes

Finally, I found a solution that works correctly for my problem :

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

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