所以我能够成功地将matplotlib嵌入到我的PyQt5程序中,除非我遇到一个问题,即我所拥有的代码似乎导致在生成该小部件的matplot期间打开和关闭matplot小部件的弹出窗口。我能够找到问题所在,但仍坚持如何解决。
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
因此,问题出在上述函数中,该函数是使用以下模块而不是以下模块的单独文件中单独类的函数:
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)
很抱歉,如果这个问题过于具体。我试图做:
plt.close()
plt.hexabin(....visible=False)
“有什么解决办法或我没有看到的东西吗? plt.close()
语句。相反,仅在需要时关闭Qt窗口。关于如何在不使用pyplot
的情况下进行集成的好例子,可以找到here。>>