background - matplotlib and transparency figure -


i working matplotlib library , pyqt5 python 3.6. add figure in window create, , wish set transparent background of figure because add image background of window. but, figure not transparent, duplicates background image of window. example, deals same problem 2 years ago : matplotlib , pyqt4 transparent background

here working example (with background black figure not black) :

import sys, os pyqt5.qtcore import qt pyqt5.qtgui import * pyqt5.qtwidgets import *  import matplotlib matplotlib.use('qt5agg') # make sure using qt5 matplotlib.backends.backend_qt5agg import figurecanvasqtagg figurecanvas matplotlib.backends.backend_qt5agg import navigationtoolbar2qt navigationtoolbar matplotlib.figure import figure import matplotlib.pyplot plt  class secondwindow(qwidget):     def __init__(self, parent=none):         super(secondwindow, self).__init__(parent)         self.setupui(self)      def setupui(self, form):         # window settings         form.setwindowtitle('hello')         self.p = qpalette()         self.pixmap = qpixmap(os.getcwd() + "/logo.png").scaled(self.size(), qt.ignoreaspectratio, qt.smoothtransformation)         self.p.setbrush(qpalette.background, qbrush(self.pixmap))         self.setpalette(self.p)          # create figure , settings         self.figure = plt.figure()         self.figure.patch.set_facecolor('none')         self.figure.patch.set_alpha(0)         self.canvas = figurecanvas(self.figure)         self.axes = self.figure.add_subplot(111)          # window layout (with h1 , h2)         self.setlayout(qvboxlayout())         self.layout().addwidget(self.canvas,1)         self.layout().setcontentsmargins(50, 50, 50, 50)  if __name__ == '__main__':     app = qapplication(sys.argv)     form = secondwindow()     form.show()     sys.exit(app.exec_()) 

i search answer during long hours didn't find solution yet. can bring !

  • operating system: windows 7 pro
  • matplotlib version: 2.0.2 (installed via anaconda, conda install matplotlib --channel conda-forge)
  • python version: python 3.6
  • anaconda 3

the problem occurs because background image set palette widget. causes canvas inherit palette , hence canvas have image background, somehow overlaying widget's background.

a solution set background of canvas transparent. easy way style sheets.

self.canvas.setstylesheet("background-color:transparent;") 

note not same setting patches' facecolor none. figure has background, controlled inside matplotlib, canvas, being pyqt object has background.

complete example:

import sys, os pyqt4.qtcore import qt pyqt4.qtgui import *  import matplotlib matplotlib.use('qt4agg')  matplotlib.backends.backend_qt4agg import figurecanvasqtagg figurecanvas matplotlib.backends.backend_qt4agg import navigationtoolbar2qt navigationtoolbar matplotlib.figure import figure import matplotlib.pyplot plt plt.rcparams['xtick.color'] ="w" plt.rcparams['ytick.color'] ="w" plt.rcparams['font.size'] = 14   class secondwindow(qwidget):     def __init__(self, parent=none):         super(secondwindow, self).__init__(parent)          # create figure , settings         self.figure = plt.figure()         self.figure.patch.set_facecolor("none")         self.canvas = figurecanvas(self.figure)         self.axes = self.figure.add_subplot(111)         self.axes.patch.set_alpha(0.5)          ###### make background of canvas transparent         self.canvas.setstylesheet("background-color:transparent;")          self.p = qpalette()         self.p.setbrush(qpalette.background, qbrush(qpixmap("house.png")))         self.setpalette(self.p)          self.setlayout(qvboxlayout())         self.layout().addwidget(self.canvas,1)         self.layout().setcontentsmargins(50, 50, 50, 50)  if __name__ == '__main__':     app = qapplication(sys.argv)     form = secondwindow()     form.show()     sys.exit(app.exec_()) 

which might like

enter image description here


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -