Draw rectangles and lines

2,990 views
Skip to first unread message

Stefanie Lück

unread,
Apr 9, 2014, 9:23:51 AM4/9/14
to pyqt...@googlegroups.com
Hello!

I am new to pyqtgraph and would like to draw a line plot and under the line plot a simple line with a rectangle (see line.jpg). The Lineplot is no problem, everything is working fine but is there any possibility to add a simple line with rectangle under the plot? It can be outside or inside the plot but the line should show up under the line of the plot (see plot.jpg).

Thank you very much in advance!
Stefanie
line.jpg
plot.jpg

Luke Campagnola

unread,
Apr 9, 2014, 10:12:48 AM4/9/14
to pyqt...@googlegroups.com
On Wed, Apr 9, 2014 at 9:23 AM, Stefanie Lück <lue...@gmail.com> wrote:
Hello!

I am new to pyqtgraph and would like to draw a line plot and under the line plot a simple line with a rectangle (see line.jpg). The Lineplot is no problem, everything is working fine but is there any possibility to add a simple line with rectangle under the plot? It can be outside or inside the plot but the line should show up under the line of the plot (see plot.jpg).

Certainly; just add a couple of QGraphicsRectItem to your view: http://qt-project.org/doc/qt-4.8/qgraphicsrectitem.html#details
There are a variety of other shapes that Qt provides as well.

If you want the shapes to appear outside the plot (but maintain vertical alignment with the data), then I would something like the following:

# Set up a window with plot
import pyqtgraph as pg
win = pg.GraphicsWindow()
plt = win.addPlot()
plt.plot(x=[0, 0.1, 0.2, 0.3, 0.4], y=[1, 7, 2, 4, 3])

# Add a ViewBox below with two rectangles
vb = win.addViewBox(col=0, row=1)
r1 = pg.QtGui.QGraphicsRectItem(0, 0, 0.4, 1)
r1.setPen(pg.mkPen(None))
r1.setBrush(pg.mkBrush('r'))
vb.addItem(r1)
r2 = pg.QtGui.QGraphicsRectItem(0.2, -5, 0.1, 10)
r2.setPen(pg.mkPen((0, 0, 0, 100)))
r2.setBrush(pg.mkBrush((50, 50, 200)))
vb.addItem(r2)

# Make the ViewBox flat
vb.setMaximumHeight(70)

# Force y-axis to be always auto-scaled
vb.setMouseEnabled(y=False)
vb.enableAutoRange(y=True)

# Force x-axis to match the plot above
vb.setXLink(plt)


Stefanie Lück

unread,
Apr 10, 2014, 2:29:10 AM4/10/14
to pyqt...@googlegroups.com
Thanks for your reply!

Is it also possible to use QGraphicsRectItem  inside a GraphicsLayout? If I do, I I am getting an error. I attached a simplified example.
Thanks
pygraph.py

Stefanie Lück

unread,
Apr 10, 2014, 3:46:05 AM4/10/14
to pyqt...@googlegroups.com
The matter has resolved itself!

I found out that I can add a ViewBox to a GraphicsLayout.

self.lay = pg.GraphicsLayout()
vb = self.lay.addViewBox(col=0, row=2)

Best regards
Reply all
Reply to author
Forward
0 new messages