How to fill a polygon?

578 views
Skip to first unread message

Robert Budzyński

unread,
May 12, 2018, 7:48:28 AM5/12/18
to pyqtgraph
I have a PlotDataItem that form a closed polygon, and I would like to fill the interior area with some color other than the background, or perhaps with some other QBrush. How to achieve this? FillBetweenItem does not seem to cover this case.

Luke Campagnola

unread,
May 12, 2018, 1:28:02 PM5/12/18
to pyqt...@googlegroups.com
Qt provides several lower-level drawing primitives. In this case, you probably want QGraphicsPathItem: http://doc.qt.io/archives/qt-4.8/qgraphicspathitem.html

You could also use pg.arrayToQPath to generate the QPath: http://www.pyqtgraph.org/documentation/functions.html#pyqtgraph.arrayToQPath


On May 12, 2018 4:48 AM, "Robert Budzyński" <dr.b...@gmail.com> wrote:
I have a PlotDataItem that form a closed polygon, and I would like to fill the interior area with some color other than the background, or perhaps with some other QBrush. How to achieve this? FillBetweenItem does not seem to cover this case.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/86fe0a56-85cc-4d19-9149-3b69bedc0081%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Budzyński

unread,
May 13, 2018, 7:26:51 AM5/13/18
to pyqtgraph
Maybe it's just me, but I find the Qt graphics API extremely confusing. I've had no luck getting this to work.

Another task I have found no way to accomplish is to position a TextItem in a fixed location relative to the visible bounds of a PlotWidget. I tried to define a resizeEvent that applies setPos,, but that leads to infinite recursion:

File "/usr/lib/python3/dist-packages/pyqtgraph/widgets/PlotWidget.py", line 75, in __getattr__

   
if hasattr(self.plotItem, attr):

 
[Previous line repeated 325 more times]

RecursionError: maximum recursion depth exceeded



On Saturday, May 12, 2018 at 7:28:02 PM UTC+2, Luke Campagnola wrote:
Qt provides several lower-level drawing primitives. In this case, you probably want QGraphicsPathItem: http://doc.qt.io/archives/qt-4.8/qgraphicspathitem.html

You could also use pg.arrayToQPath to generate the QPath: http://www.pyqtgraph.org/documentation/functions.html#pyqtgraph.arrayToQPath

On May 12, 2018 4:48 AM, "Robert Budzyński" <dr.b...@gmail.com> wrote:
I have a PlotDataItem that form a closed polygon, and I would like to fill the interior area with some color other than the background, or perhaps with some other QBrush. How to achieve this? FillBetweenItem does not seem to cover this case.

--
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.

Robert Budzyński

unread,
May 14, 2018, 11:55:44 AM5/14/18
to pyqtgraph


On Saturday, May 12, 2018 at 7:28:02 PM UTC+2, Luke Campagnola wrote:
Qt provides several lower-level drawing primitives. In this case, you probably want QGraphicsPathItem: http://doc.qt.io/archives/qt-4.8/qgraphicspathitem.html

You could also use pg.arrayToQPath to generate the QPath: http://www.pyqtgraph.org/documentation/functions.html#pyqtgraph.arrayToQPath



More specifically, this QPainterPath should be painted on some QWidget, and I can't figure out which. My guess was the ViewBox, but I get:

TypeError: arguments did not match any overloaded call:
 QPainter(): too many arguments
 QPainter(QPaintDevice): argument 1 has unexpected type 'ViewBox'

 

Luke Campagnola

unread,
May 15, 2018, 5:16:31 PM5/15/18
to pyqt...@googlegroups.com
No need to do any manual painting. To be more specific:

1. Use arrayToQPath to generate a QPainterPath object (this describes just the coordinates of lines in the path)
2. Create a QGraphicsPathItem using the QPainterPath as its input
3. Add the QGraphicsPathItem to the ViewBox or PlotItem
4. Probably you will also need to set the pen/brush on the QGraphicsPathItem

Text can be added anywhere in much the same way, with QGraphicsTextItem. The important thing here, though, is that you do not want to add the text into the viewbox like you did with the path (because you don't want the text to scale/translate with the view). Instead, make the ViewBox or PlotItem the parent of the text item. For example:

    plt = pg.plot()
    text = pg.QtGui.QGraphicsTextItem("Hello")
    text.setDefaultTextColor(pg.mkColor('w'))
    text.setParentItem(plt.plotItem)   # this is not the same as plt.plotItem.addItem(text)



--
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+unsubscribe@googlegroups.com.

Robert Budzyński

unread,
May 16, 2018, 5:56:25 AM5/16/18
to pyqtgraph
Thank you Luke, this does mostly work.
However, I understand that to position an item (like text) relative to, say, the lower-right corner of the visible widget, requires tampering with an event handler? Or a signal slot?


On Tuesday, May 15, 2018 at 11:16:31 PM UTC+2, Luke Campagnola wrote:
No need to do any manual painting. To be more specific:

1. Use arrayToQPath to generate a QPainterPath object (this describes just the coordinates of lines in the path)
2. Create a QGraphicsPathItem using the QPainterPath as its input
3. Add the QGraphicsPathItem to the ViewBox or PlotItem
4. Probably you will also need to set the pen/brush on the QGraphicsPathItem

Text can be added anywhere in much the same way, with QGraphicsTextItem. The important thing here, though, is that you do not want to add the text into the viewbox like you did with the path (because you don't want the text to scale/translate with the view). Instead, make the ViewBox or PlotItem the parent of the text item. For example:

    plt = pg.plot()
    text = pg.QtGui.QGraphicsTextItem("Hello")
    text.setDefaultTextColor(pg.mkColor('w'))
    text.setParentItem(plt.plotItem)   # this is not the same as plt.plotItem.addItem(text)


On Mon, May 14, 2018 at 8:55 AM, Robert Budzyński <dr.b...@gmail.com> wrote:


On Saturday, May 12, 2018 at 7:28:02 PM UTC+2, Luke Campagnola wrote:
Qt provides several lower-level drawing primitives. In this case, you probably want QGraphicsPathItem: http://doc.qt.io/archives/qt-4.8/qgraphicspathitem.html

You could also use pg.arrayToQPath to generate the QPath: http://www.pyqtgraph.org/documentation/functions.html#pyqtgraph.arrayToQPath



More specifically, this QPainterPath should be painted on some QWidget, and I can't figure out which. My guess was the ViewBox, but I get:

TypeError: arguments did not match any overloaded call:
 QPainter(): too many arguments
 QPainter(QPaintDevice): argument 1 has unexpected type 'ViewBox'

 

--
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.

Robert Budzyński

unread,
May 16, 2018, 7:19:38 AM5/16/18
to pyqtgraph
Okay, as for the text: here's what works best for my purpose (I want to, so to speak, annotate the PlotItem rather than the view of the plot - i.e. I want the text positioned outside the axes and relative to the edge of the widget)


clabel = pg.LabelItem('some text...', justify='right')
widget
.plotItem.layout.addItem(clabel, 4, 1)


where widget is my PlotWidget.

The only puzzle is that using Qt layout flags in the addItem call seems to have no effect. 
Reply all
Reply to author
Forward
0 new messages