Discussion:
Draw rectangles and lines
Stefanie Lück
2014-04-09 13:23:51 UTC
Permalink
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
--
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-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/4c15ffe8-0bce-42de-baec-89b21ce8afd6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Luke Campagnola
2014-04-09 14:12:48 UTC
Permalink
Post by Stefanie Lück
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)
--
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-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/CACZXET92MEumvGG%3Dp3CuF8w6%3DVnL5iQ9sVAxs0i3xicR7KSx6w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Stefanie Lück
2014-04-10 06:29:10 UTC
Permalink
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
Post by Luke Campagnola
Post by Stefanie Lück
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).
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
# 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)
--
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-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/f229f0fb-acaa-46be-8851-c44f079d8d67%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stefanie Lück
2014-04-10 07:46:05 UTC
Permalink
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
Post by Stefanie Lück
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
Post by Luke Campagnola
Post by Stefanie Lück
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).
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
# 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)
--
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-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/3f50d53b-60e3-4dfd-b994-1d6372a32284%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...