Transperent background of plots

80 views
Skip to first unread message

Daniele Dolci

unread,
Apr 11, 2020, 12:11:31 PM4/11/20
to pyqtgraph
Hi!

I have a main widget with a layout and a gradient as background. I want to add a series of plots with transperent background (basically I would like to only draw the foreground). I am having hardtimes making the background of the plot transperent. I tried lot of things, with this code I was expecting that I would be making all transparent, but it is actually white:

x = [x for x in range(1000)]
y = [x for x in range(1000)]
plotWidget = pg.PlotWidget(title="Ph")
plotWidget.setBackground((0, 0, 0, 0))
plotWidget.getViewBox().setBackgroundColor((0, 0, 0, 0))
self._tab_layout.addWidget(plotWidget, 0, 0)
pen = pg.mkPen(color=(255, 0, 0))
t = plotWidget.plot(x, y, pen=pen)
t.getViewBox().setBackgroundColor((0, 0, 0, 0))

So it looks like this, and I would:

plot1.jpg


And I would like to have it drawn on this background instead:

plot2.jpg



Any idea how to fix this?

Thanks!

Israel Brewster

unread,
Apr 11, 2020, 12:20:06 PM4/11/20
to pyqtgraph
Try:

plotWidget.setBackgroundBrush(QBrush(Qt.transparent, Qt.NoBrush))

There may be some other code needed as well, but that’s at least part of it. you *may* need to do the same for the view box. So you had the right idea, but perhaps not quite right?
---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320

On Apr 11, 2020, at 8:11 AM, Daniele Dolci <daniele...@gmail.com> wrote:

Hi!

I have a main widget with a layout and a gradient as background. I want to add a series of plots with transperent background (basically I would like to only draw the foreground). I am having hardtimes making the background of the plot transperent. I tried lot of things, with this code I was expecting that I would be making all transparent, but it is actually white:

x = [x for x in range(1000)]
y = [x for x in range(1000)]
plotWidget = pg.PlotWidget(title="Ph")
plotWidget.setBackground((0, 0, 0, 0))
plotWidget.getViewBox().setBackgroundColor((0, 0, 0, 0))
self._tab_layout.addWidget(plotWidget, 0, 0)
pen = pg.mkPen(color=(255, 0, 0))
t = plotWidget.plot(x, y, pen=pen)
t.getViewBox().setBackgroundColor((0, 0, 0, 0))

So it looks like this, and I would:

<plot1.jpg>


And I would like to have it drawn on this background instead:

<plot2.jpg>



Any idea how to fix this?

Thanks!

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/2e9b649e-dc5e-4a92-ad3f-b6b9cfb6226d%40googlegroups.com.
<plot1.jpg><plot2.jpg>

Daniele Dolci

unread,
Apr 11, 2020, 12:37:33 PM4/11/20
to pyqtgraph
Thanks for the fast reply! 

I tried to add that line but didn't work. I then tried to apply it to the plot item and to the view box, but on both I get the error:

plotWidget.getViewBox().setBackgroundBrush(QtGui.QBrush(QtCore.Qt.transparent, QtCore.Qt.NoBrush))
AttributeError: 'ViewBox' object has no attribute 'setBackgroundBrush'

Maybe I misunderstood what you meant by doing the same for the ViewBox?

Thanks again! 
Try:

plotWidget.setBackgroundBrush(QBrush(Qt.transparent, Qt.NoBrush))

To unsubscribe from this group and stop receiving emails from it, send an email to pyqt...@googlegroups.com.

Israel Brewster

unread,
Apr 11, 2020, 1:42:31 PM4/11/20
to pyqtgraph
No, I mis-spoke with that. I took another look at my code, and realized that I’m not doing *anything* with the view box. So try just doing the setBackgroudBrush on the plotWidget, but *don’t* set the background color on the view box at all. If that doesn’t work, then I’m probably missing something somewhere else in my code that does it…

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/3c60ca81-99f3-4c4c-9a4e-7cd22d7f9335%40googlegroups.com.

Daniele Dolci

unread,
Apr 11, 2020, 1:51:37 PM4/11/20
to pyqtgraph
So I tried this:

x = [x for x in range(1000)]
y = [x for x in range(1000)]
plotWidget = pg.PlotWidget(title="Ph")
plotWidget.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.transparent, QtCore.Qt.NoBrush))
self._tab_layout.addWidget(plotWidget, 0, 0)
t = plotWidget.plot(x, y)

But the background is still white. I removed all the viewBox stuff and the set colors. I mean, this is very weird. If the plot accepts colors for pen and background in the format of RGBa, why wouldn't the transperency do the trick?

I have also tried using just plotItem, without the widget, but I wasn't able to add it to the layout so I couldn't really finish the test. 

Thanks for your time man, I appreciate!
Cheers,

Israel Brewster

unread,
Apr 11, 2020, 1:58:36 PM4/11/20
to pyqtgraph
One thought that just occurred to me: I’m using the latest development version of pyqtgraph. Are you by any chance on the release version? That could be the difference, theoretically, if so.

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/4c71a8d2-5d8c-41a7-a3d2-e5a9dc106117%40googlegroups.com.

Daniele Dolci

unread,
Apr 11, 2020, 2:06:50 PM4/11/20
to pyqtgraph
I would assume I am on the latest version, I installed it today. One difference perhaps might be the operative system? I am using raspbian OS.

To install it I used

sudo apt install python3-pyqtgraph

So I assume it gets the latest one, or not?

Israel Brewster

unread,
Apr 11, 2020, 2:09:40 PM4/11/20
to pyqtgraph
Nope, that would be the latest *release* version, which would be 0.10. I’m using the latest *development* version that you can get from source forge. So yeah, that *could* be why my code isn’t working for you (though I don’t guarantee it).

There have been a lot of changes between the last release and the current development (like compatibility with PySide2, which is why I use the development). I keep wondering when they will make a new release, but it hasn’t happened yet.

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/4d6a1fdc-0df4-435e-a362-1e5b8e0328fb%40googlegroups.com.

Daniele Dolci

unread,
Apr 11, 2020, 3:42:11 PM4/11/20
to pyqtgraph
Alright, tomorrow I will try download the development version then and give it a try. 

I will let you know how it goes, thanks!

Cheers,

Daniele Dolci

unread,
Apr 12, 2020, 11:22:03 AM4/12/20
to pyqtgraph
Hi!

I gave it a try but still same result. I uninstalled everything with 

sudo pip3 uninstall pyqtgraph


and made sure that under dist-packages there was nothing left. Then I tried to download it from source forge but with no much success, finally, as is read here https://github.com/pyqtgraph/pyqtgraph , I used the command:


or, actually, 
 
sudo pip3 install git+https://github.com/pyqtgraph/pyqtgraph@develop

Any other idea of what I could try? 

Cheers,

Israel Brewster

unread,
Apr 12, 2020, 11:51:38 AM4/12/20
to pyqtgraph
I’ll have to find some time to go back over my code and see what I’m missing then, because it’s working for me. Dunno off the top of my head. Sorry!

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/770c9bdc-6fd4-46bc-a1fd-64e212790413%40googlegroups.com.

Daniele Dolci

unread,
Apr 12, 2020, 11:59:37 AM4/12/20
to pyqt...@googlegroups.com
I understand, I appreciated the help you gave so far. Is it possible it has to do with some css? 

Thanks!



--
Daniele Dolci
Technical Director & Python Developer
Vetralla, Italy
Skype: dolci.daniele
Portfolio (YouTube): https://youtu.be/IMZdW4ikjok
=======================================
Insanity:  doing the same thing over and over again and expecting different results. 
- Albert Einstein - 

Israel Brewster

unread,
Apr 12, 2020, 1:16:12 PM4/12/20
to pyqtgraph
So here’s a short snippet of code that works for me to get a plot with a transparent background:

from PySide2.QtWidgets import QApplication, QDialog, QVBoxLayout
from PySide2.QtGui import QBrush
from PySide2.QtCore import Qt
import pyqtgraph as pg

if __name__ == "__main__":
    app=QApplication()
    dialog=QDialog()
    dialog.setStyleSheet('background-color:#050’)
    
    layout=QVBoxLayout(dialog)
    dialog.setLayout(layout)
    
    vals=list(range(100))
    
    plotWidget=pg.PlotWidget(dialog)
    plotWidget.setBackgroundBrush(QBrush(Qt.transparent,Qt.NoBrush))
    plotWidget.plot(vals)
    
    layout.addWidget(plotWidget)
    
    dialog.show()
    
    app.exec_()

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Message has been deleted

Daniele Dolci

unread,
Apr 12, 2020, 3:35:20 PM4/12/20
to pyqtgraph
Hi! It was actually easier than I expected. A couple of css line did the trick. Just set the objectName of the plot widget and then I could assign a background-color: rgba(x, x, x, 0);



plots.jpg





Thanks anyway for the help! 

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqt...@googlegroups.com.


--
Daniele Dolci
Technical Director & Python Developer
Vetralla, Italy
Skype: dolci.daniele
Portfolio (YouTube): https://youtu.be/IMZdW4ikjok
=======================================
Insanity:  doing the same thing over and over again and expecting different results. 
- Albert Einstein - 


--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/CAF_JKbCxJK1NUrcrJnJ0m8eLfAHGt11POh5knCPmLwL3--d%3D3A%40mail.gmail.com.

Israel Brewster

unread,
Apr 12, 2020, 3:50:59 PM4/12/20
to pyqtgraph
Interesting. As you can see from my code snipet, I didn’t have to do any of that. But as long as it works, right? :-) Good job figuring it out!

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
On Apr 12, 2020, at 11:35 AM, Daniele Dolci <daniele...@gmail.com> wrote:

Hi! It was actually easier than I expected. A couple of css line did the trick. Just set the objectName of the plot widget and then I could assign a background-color: rgba(x, x, x, 0);



To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/a97a376c-c8e9-403d-9cb5-b733fb773594%40googlegroups.com.
<plots.jpg>

Reply all
Reply to author
Forward
0 new messages