2

I need the whole plot window to be transparent so that a chrome window, for example, on my desktop could be seen through the plot, so that I can add points to it while seeing what's behind it.

https://stackoverflow.com/a/45505906/13650485

The answer I've listed above is EXACTLY what I want to do, except my interactive system doesn't work with TK. I'd like to use Qt5Agg. When I run the code above, the system won't accept it -- it says QT5 is currently running. If I run it without QT already loaded, it creates a blank transparent window (yay!) but if I move it or click on the icon it turns opaque black without any plot. If I change tk to Qt5 it complains on lift. If I remove the "win" code, it has no transparency(obviously). I've tried adding everything I can think of to make the canvas transparent and I can change the color but not make it transparent.


import matplotlib
# make sure Tk backend is used
matplotlib.use("TkAgg")  
import matplotlib.pyplot as plt

# create a figure and some subplots
fig, ax = plt.subplots(figsize=(4,2))
ax.plot([2,3,5,1])
fig.tight_layout()

win = plt.gcf().canvas.manager.window

win.lift()
win.attributes("-topmost", True)
win.attributes("-transparentcolor", "white")

plt.show()

When I made the changes suggested by: eyllanesc

I found within a vanilla Spyder 4.1.3 | Python 3.7.7 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10

In order to import QtCore I had to first

  • conda install pyqt

  • not enough, so then conda install pyqt5

  • and also conda update --all

When I did that, the code ran without errors. This is a better first result!, but I still only get the frozen mpl.fig window. This time, however, it is white. . . The console returns, but the mpl window hangs. Run again, a new frozen window. Restart and run again: same result.

I hope that this is a simple error; please teach this newby.

@eyllanesc
Revised: Python screen tracing application – needs a mostly transparent plot window. I need the whole plot window to be transparent so that a chrome window, for example, on my desktop could be seen through the plot, so that I can add plot (x, y) points to it while seeing what's behind it.

Adding the command win.setWindowFlags(QtCore.Qt.FramelessWindowHint) did indeed make the window transparent, but it made the tool bar transparent, got rid of the title bar, and removed the ability to move or resize the window. It also made it so that the graph area was not sensitive to the mouse unless I was over the line. I added the facecolor attribute to the subplots command so I could see what was going on. As long as I put a non-zero value for either the fig-alpha or the ax-alpha, the graph is sensitive to the mouse over the whole area.

I need to be able to move and resize the window and would like to have the toolbar be opaque or at least sensitive to the mouse over the whole toolbar. Can you help with this? Thanks for past help!

## Python Code Fragment by Helen for Windows 10
## to test sequence creating plot with transparent
## background (to be used to trace and record xy pairs)

from PyQt5 import QtCore
import matplotlib
matplotlib.use("Qt5Agg")  #define backend, must be before pyplot is imported
import matplotlib.pyplot as plt

# create a figure and a subplot
fig,ax = plt.subplots(figsize=(4, 2),facecolor=(1.,1.,0.,0.1)) #facecolor of figure

fig.patch.set_alpha(0.1) 
ax.patch.set_alpha(0.1)

# plot some fixed points
ax.plot([2, 3, 5, 1])
fig.tight_layout()

#make window transparent to the desktop
win = plt.gcf().canvas.manager.window
win.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
win.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
win.setStyleSheet("background:transparent")
win.setWindowFlags(QtCore.Qt.FramelessWindowHint) 
win.setWindowTitle("My App")

plt.show()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Helen
  • 23
  • 4

1 Answers1

6

You have to use the Qt flags, tested on Linux:

from PyQt5 import QtCore


import matplotlib

# make sure Tk backend is used
matplotlib.use("Qt5Agg")
import matplotlib.pyplot as plt

# create a figure and some subplots
fig, ax = plt.subplots(figsize=(4, 2))
fig.patch.set_alpha(0.0)
ax.patch.set_alpha(0.0)

ax.plot([2, 3, 5, 1])
fig.tight_layout()

win = plt.gcf().canvas.manager.window
win.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
win.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
win.setStyleSheet("background:transparent")

plt.show()

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • +1 for quick response, and a tested and demonstrated one. If I could do another +1, it would be for your repo of translated qt examples. (https://github.com/eyllanesc/QtExamples) Teaching to fish. Well done. – ElderDelp May 31 '20 at 04:12
  • I tried it on Linux, and it works there just as you showed. I wonder why windows does not do the same. Thoughts anyone? – ElderDelp Jun 01 '20 at 00:20
  • @ElderDelp Transparency is OS dependent, in windows for a Qt window to be transparent then native titlebar should not be used: add `win.setWindowFlags(QtCore.Qt.FramelessWindowHint)` and tell me if it works. – eyllanesc Jun 01 '20 at 00:23
  • I added this line after after the win.setStyleSheet line of code. Now I get a transparent window with a title "figure 1", an icon on top left and the delete X on top right. If I click on the window it turns black. I can't move it or stretch it. If I type %matplotlib qt in the console before running it, bkgrnd is black with the graph, top has left and rt arrows, don't work, i can't move the window, but I can stretch it from lower right. I can delete the window from the icon on the control strip. – Helen Jun 01 '20 at 03:07
  • I really appreciate your quick response! THank you. – Helen Jun 01 '20 at 03:08
  • @Helen Have you tested it from the console or do you use a special environment? – eyllanesc Jun 01 '20 at 03:18
  • @eyllanesc I thought I had replied to your last question but must have made a mistake sending it. I’ve had many interruptions. The last question that asked-- was I testing from the console or a special environment?-- clued me in to a problem. I was running from the spyder console using anaconda and apparently I was having problems with the different environments fighting over control of the mouse. Now I am running using IDLE and behavior is more predictable. – Helen Sep 29 '20 at 03:37
  • I get a `AttributeError: 'FigureManagerBase' object has no attribute 'window'` error. What am I missing here? – Meet Oct 25 '21 at 09:28