0

So I was able to successfully embed matplotlib into my PyQt5 program, except I am running into a problem where it seems the code I have is causing a popup of a matplot widget to open and close during the generation of the matplot for the widget. I was able to source the problem, but I am stuck on how I can go about to fix it.

def getHexabinData(self, shotsDf):
    #returns the object type of the shot / makes hexabin
    shotsHex = plt.hexbin(-shotsDf.LOC_X, shotsDf.LOC_Y, 
        extent=(-250, 250, 422.5, -47.5), cmap='Blues', gridsize=45, marginals=True, visible=False)
    print('done')
    #grabs object of hexabin of all shots
    makeDf = shotsDf[shotsDf.SHOT_MADE_FLAG == 1]
    #grabs the data frame of all the makes
    makesHex = plt.hexbin(-makeDf.LOC_X, makeDf.LOC_Y,
        extent=(-250, 250, 422.5, -47.5), cmap=plt.cm.Reds, gridsize=45, marginals=True, visible=False)
    print('done')
    plt.close()
    #close the hexabin plot
    pctsByHex = np.true_divide(makesHex.get_array(), shotsHex.get_array())
    pctsByHex[np.isnan(pctsByHex)] = 0  # convert NAN values to 0
    sizesByHex = len(shotsHex.get_array()) * [0]
    sizesByHex = self.getSizeHexByZone(shotsDf, sizesByHex)
    sizesByHex = sizesByHex * 120
    #size 210 for figsize(12,11)
    print('hexes done')
    return shotsHex, pctsByHex, sizesByHex

And so, I've sourced the problem to be in the function above, which is a function of a separate class in a separate file that uses the following module instead of:

import matplotlib.pyplot as plt
#instead of these imported modules below for the pyqt5 program
from matplotlib.patches import Circle, Rectangle, Arc
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)

Apologies if this question is way too specific of a problem. I've tried to do:

plt.close()
plt.hexabin(....visible=False)

but I still get this random "matplot" widget popup that opens and closes itself until the matplot widget shows the updated plot. Is there any fix to this or something I am not seeing?

Lukar Huang
  • 77
  • 1
  • 4
  • You have asked several questions here, and 1 on Sports.SE, but have never voted. Why is that? – Nike Aug 10 '20 at 17:19

1 Answers1

0

Do not use import matplotlib.pyplot as plt when you integrate Matplotlib in PyQt. The pyplot module has its own event loop and maintains its own list of windoww. This clashes with PyQt as you are now are experiencing.

So remove the plt.close() statement. Instead just close the Qt window when needed.

A good example on how to integrate without using pyplot can be found here.

titusjan
  • 5,376
  • 2
  • 24
  • 43
  • Gotchu, that explains why I have an extra matplot widget that pops up. I guess my question would then be how I could avoid using 'import matplotlib.pyplot as plt' in this class. I already have matplotlib embed into my PyQt5 already, it's just that the backend calculations is causing this Qt window of another matplot widget to open up before I pass in the final parameters for matplot widget I already have embed into PyQt if that makes sense. Apologies for a very specific question, very new to programming. – Lukar Huang Apr 06 '20 at 03:13
  • Actually,it is very hard to understand from a description and a few snippets exactly what is going on. The only way to get further help is that you construct a proper [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Please edit your post and add an **complete**, but **minimal**, program that we can just copy-paste-execute. A good way to make an MRE is to start with your program and remove everything that is not related to the issue at hand. Be rigorous about it. E.g. in your case it doesn't matter what kind of data is plotted, so remove all calculations and plot test data. – titusjan Apr 06 '20 at 10:37