I'm using pyqtgraph to plot graphics where I put some text (for example the max of the data...)
My question is how to know the size (height, width) of a TextItem input? It's for controling its place for avoiding being out of the window.
For example my code could be this :
# -*- coding: utf-8 -*-
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
pg.setConfigOptions(antialias=True)
voieTT=np.ones(100)
voieTemps=np.ones(100)
for i in range(100):
voieTT[i]=np.random.random_sample()
voieTemps[i]=i
signemax=np.max(voieTT)
tmax=np.where(voieTT==np.max(voieTT))[0][0]
grapheTT = win.addPlot(enableMenu=False)
grapheTTVB=grapheTT.getViewBox()
grapheTT.plot(voieTemps, voieTT)
grapheTT.plot([tmax],[signemax],symbol='o',symbolSize=8)
grapheTT.showGrid(x=True, y=True)
textVmax = pg.TextItem(anchor=(0.5,1.5), border='w', fill=(0,0,255))
textVmax.setHtml('<div style="text-align: center"><span style="color: #FFF;">Vmax=%0.1f mm/s # %0.1f s</span></div>'%(np.abs(signemax),tmax))
grapheTT.addItem(textVmax)
textVmax.setPos(tmax, signemax)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
#changed original code to simple plot() because manual window structure
#somehow didn't automatically cooperate with viewbox' map methods
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
voieTT=np.ones(100)
voieTemps=np.ones(100)
for i in range(100):
voieTT[i]=np.random.random_sample()
voieTemps[i]=i
signemax=np.max(voieTT)
tmax=np.where(voieTT==np.max(voieTT))[0][0]
grapheTT = pg.plot(enableMenu=False)
grapheTTVB=grapheTT.getViewBox()
grapheTT.plot(voieTemps, voieTT)
grapheTT.plot([tmax],[signemax],symbol='o',symbolSize=8)
grapheTT.showGrid(x=True, y=True)
textVmax = pg.TextItem(anchor=(0.5,1.5), border='w', fill=(0,0,255))
textVmax.setHtml('<div style="text-align: center"><span style="color: #FFF;">Vmax=%0.1f mm/s # %0.1f s</span></div>'%(np.abs(signemax),tmax))
grapheTT.addItem(textVmax)
textVmax.setPos(tmax, signemax)
#DISCLAIMER: there's likely a better way, but this works too:
#you can get pixel dimensions x,y,w,h from bounding rectangle
br = textVmax.boundingRect()
print br
#and scale them into viewbox data dimensions
sx, sy = grapheTTVB.viewPixelSize() #scaling factors
print sx, sy
print sx*br.width(), sy*br.height()
#ALSO, you can just use ViewBox autorange
#ViewBox autoranges based on its contained items' dataBounds() info
#however TextItem.py is a UIGraphicsItem.py with dataBounds() return None
"""Called by ViewBox for determining the auto-range bounds.
By default, UIGraphicsItems are excluded from autoRange."""
#so, following example of... ArrowItem.py
#here is a simple dataBounds() for TextItem
def Autoranging_TextItem_dataBounds(axis, frac=1.0, orthoRange=None):
br = textVmax.boundingRect()
sx, sy = grapheTTVB.viewPixelSize()
w, h = sx*br.width(), sy*br.height()
if axis == 0: return [-w/2, w/2]
if axis == 1: return [0, h]
textVmax.dataBounds = Autoranging_TextItem_dataBounds
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Related
I want to dynamically rotate TextItem but cannot get it to work. Changing position or anchor with setPos and setAnchor updates the item, but wanting to change angel with setAngle doesn't update the text. The strangest thing is that it does update once I drag the canvas.
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
app = QtGui.QApplication([])
w = pg.GraphicsView()
w.show()
w.resize(800,800)
view = pg.ViewBox()
w.setCentralItem(view)
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 200, 200))
anchor = pg.TextItem()
anchor.setText("hey")
anchor.setColor(QtGui.QColor(255, 255, 255))
view.addItem(anchor)
def rotate():
x, y = anchor.pos()
anchor.setPos(x + 1, y + 1)
anchor.setAngle(anchor.angle + 10)
timer = QtCore.QTimer()
timer.timeout.connect(rotate)
timer.start(1000)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
I am wondering what signal or function to call so that the item updates immediately.
It ended up being a bug. I solved it by just removing the old TextItem and creating a new one with the updatet angle.
I use pyqtgraph to draw the candles, but if I zoom graph very much, image hide.
I noticed that when zoom very much, “paint” method stops being called. Code with http request to exchange below.
image example
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
from PyQt5 import QtWidgets
class CandlestickItem(QtWidgets.QGraphicsRectItem):
def __init__(self, data):
super(CandlestickItem, self).__init__()
self.data = data
self.generatePicture()
def generatePicture(self):
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
w = (self.data[1][0] - self.data[0][0]) / 3.
for (t, open, close, min, max) in self.data:
if max != min:
p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
if open > close:
p.setBrush(pg.mkBrush('r'))
else:
p.setBrush(pg.mkBrush('g'))
p.drawRect(QtCore.QRectF(t - w, open, w * 2, close - open))
p.end()
def paint(self, p, *args):
print('paint call')
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())
if __name__ == '__main__':
import sys
import urllib.request
import json
get_symbols_url = 'https://api.hitbtc.com/api/2/public/candles/BCNBTC?period=M30&limit=1000'
response = urllib.request.urlopen(get_symbols_url)
request_data = json.loads(response.read())
data_list = []
for i in range(0, len(request_data)):
data_list.append((float(i), float(request_data[i]['open']), float(request_data[i]['close']),
float(request_data[i]['min']), float(request_data[i]['max'])))
item = CandlestickItem(data_list)
plt = pg.plot()
plt.addItem(item)
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
I need to display very small values, for example, 0.0000001-0.00000015. What can I do to prevent image hidden at high zoom?
I was answered on the support forum and it works for me.
question on forum
It seems to be a rounding error. Need replace method of boundingRect.
# import numpy as np
def boundingRect(self):
data = np.array(self.data)
xmin = data[:,0].min()
xmax = data[:,0].max()
ymin = data[:,1:].min()
ymax = data[:,1:].max()
return QtCore.QRectF(xmin, ymin, xmax-xmin, ymax-ymin)
I'm using pyqtgraph to plot tracks of a robot (the path that the bot drove). Now I want to add a marker to the plot to indicate the bots current position and heading. I thought ArrowItem would be the right choice, because it is scale invariant and can be rotated easily. However the local origin of the arrow is at its tip like this
but I want it to be in the center like this
How can I do that? I would also appreciate different solutions to this problem.
Update
After applying eyllansec's code I get some rendering problems. A minimal example (one has to zoom or move the view to disable the auto scaling):
import pyqtgraph as pg
import numpy as np
import time
class CenteredArrowItem(pg.ArrowItem):
def paint(self, p, *args):
p.translate(-self.boundingRect().center())
pg.ArrowItem.paint(self, p, *args)
if __name__ == '__main__':
app = pg.QtGui.QApplication([])
window = pg.GraphicsWindow(size=(1280, 720))
window.setAntialiasing(True)
tracker = window.addPlot(title='Tracker')
while True:
for i in range(300):
arrow = CenteredArrowItem(angle=i, headLen=40, tipAngle=45, baseAngle=30)
arrow.setPos(i / 300, i / 300)
tracker.addItem(arrow)
app.processEvents()
time.sleep(0.02)
tracker.removeItem(arrow)
As you may noticed I'm adding and removing the arrow each iteration. This is because arrow.setStyle(angle=i) is not working as it does not update the rotation of the arrow (probably a bug).
A possible solution is to overwrite the paint method of ArrowItem and move the QPainter:
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
class MyArrowItem(pg.ArrowItem):
def paint(self, p, *args):
p.translate(-self.boundingRect().center())
pg.ArrowItem.paint(self, p, *args)
app = QtGui.QApplication([])
w = QtGui.QMainWindow()
p = pg.PlotWidget()
p.showGrid(x = True, y = True, alpha = 0.3)
w.show()
w.resize(640, 480)
w.setCentralWidget(p)
w.setWindowTitle('pyqtgraph example: Arrow')
a = pg.ArrowItem(angle=-160, tipAngle=60, headLen=40, tailLen=40, tailWidth=20, pen={'color': 'w', 'width': 3}, brush='r')
b = MyArrowItem(angle=-160, tipAngle=60, headLen=40, tailLen=40, tailWidth=20, pen={'color': 'w', 'width': 3})
a.setPos(10,0)
b.setPos(10,0)
p.addItem(a)
p.addItem(b)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
As shown in the following figure, the red arrow is the default ArrowItem, and the blue is the offset, both are located in the same position with respect to the plot.
Update:
The problem is caused by the method that rotates the item used as the center of coordinates using the center of transformations by default, that is to say the (0, 0), we must move it:
import pyqtgraph as pg
import numpy as np
import time
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph import functions as fn
class CenteredArrowItem(pg.ArrowItem):
def setStyle(self, **opts):
# http://www.pyqtgraph.org/documentation/_modules/pyqtgraph/graphicsItems/ArrowItem.html#ArrowItem.setStyle
self.opts.update(opts)
opt = dict([(k,self.opts[k]) for k in ['headLen', 'tipAngle', 'baseAngle', 'tailLen', 'tailWidth']])
tr = QtGui.QTransform()
path = fn.makeArrowPath(**opt)
tr.rotate(self.opts['angle'])
p = -path.boundingRect().center()
tr.translate(p.x(), p.y())
self.path = tr.map(path)
self.setPath(self.path)
self.setPen(fn.mkPen(self.opts['pen']))
self.setBrush(fn.mkBrush(self.opts['brush']))
if self.opts['pxMode']:
self.setFlags(self.flags() | self.ItemIgnoresTransformations)
else:
self.setFlags(self.flags() & ~self.ItemIgnoresTransformations)
if __name__ == '__main__':
app = pg.QtGui.QApplication([])
window = pg.GraphicsWindow(size=(1280, 720))
window.setAntialiasing(True)
tracker = window.addPlot(title='Tracker')
while True:
for i in range(300):
arrow = CenteredArrowItem(angle=i, headLen=40, tipAngle=45, baseAngle=30)
arrow.setPos(i / 300, i / 300)
tracker.addItem(arrow)
app.processEvents()
time.sleep(0.02)
tracker.removeItem(arrow)
After digging through the source code of pyqtgraph I ended up with a special function that suits my needs. I apply the translation when creating the arrow path, instead when rendering it. Fortunately this also solves the roation bug (for whatever reason).
Example:
import pyqtgraph as pg
import numpy as np
import time
import pyqtgraph.functions
class CenteredArrowItem(pg.ArrowItem):
def setData(self, x, y, angle):
self.opts['angle'] = angle
opt = dict([(k, self.opts[k]) for k in ['headLen', 'tipAngle', 'baseAngle', 'tailLen', 'tailWidth']])
path = pg.functions.makeArrowPath(**opt)
b = path.boundingRect()
tr = pg.QtGui.QTransform()
tr.rotate(angle)
tr.translate(-b.x() - b.width() / 2, -b.y() - b.height() / 2)
self.path = tr.map(path)
self.setPath(self.path)
self.setPos(x, y)
if __name__ == '__main__':
app = pg.QtGui.QApplication([])
window = pg.GraphicsWindow(size=(1280, 720))
window.setAntialiasing(True)
tracker = window.addPlot(title='Tracker')
arrow = CenteredArrowItem(headLen=40, tipAngle=45, baseAngle=30)
tracker.addItem(arrow)
tracker.addItem(pg.InfiniteLine(pos=(0,0), angle=45))
center = pg.ScatterPlotItem([], [], brush='r')
tracker.addItem(center)
while True:
for i in range(300):
arrow.setData(i, i, i)
center.setData([i], [i])
app.processEvents()
time.sleep(0.02)
I want to synchronize the X-Axis of several pyqtgraph plots. When the user rescales the X-Axis with mouse interactions (e.g. scroll-wheel while mouse on x-Axis) I want to assign the same changes to all the other plots. So how do I do this?
I derived a minimized code from a basic example below.
Do I have to overwrite the viewRangeChanged() functions of w1 and w2?
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.console
import numpy as np
from pyqtgraph.dockarea import *
win = QtGui.QMainWindow()
area = DockArea()
win.setCentralWidget(area)
win.resize(1000,500)
win.setWindowTitle('pyqtgraph example: dockarea')
d1 = Dock("Dock1")
d2 = Dock("Dock2")
area.addDock(d1, 'bottom')
area.addDock(d2, 'bottom', d1)
w1 = pg.PlotWidget(title="Dock 1 plot")
w1.plot(np.random.normal(size=100))
d1.addWidget(w1)
w2 = pg.PlotWidget(title="Dock 2 plot")
w2.plot(np.random.normal(size=100))
d2.addWidget(w2)
win.show()
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
This question has a follow up here with another answer to this question.
We need to use the sigRangeChanged signal and connect it to a slot, the problem is that the change of the range another item would generate the signal sigRangeChanged and so on generating an infinite loop, to solve this you must disconnect those signals before making the modifications and reconnect them to the final.
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
def onSigRangeChanged(r):
w1.sigRangeChanged.disconnect(onSigRangeChanged)
w2.sigRangeChanged.disconnect(onSigRangeChanged)
if w1 == r:
w2.setRange(xRange=r.getAxis('bottom').range)
elif w2 == r:
w1.setRange(xRange=r.getAxis('bottom').range)
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
Example:
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
from pyqtgraph.dockarea import *
import sys
def onSigRangeChanged(r):
w1.sigRangeChanged.disconnect(onSigRangeChanged)
w2.sigRangeChanged.disconnect(onSigRangeChanged)
if w1==r:
w2.setRange(xRange=r.getAxis('bottom').range)
elif w2 == r:
w1.setRange(xRange=r.getAxis('bottom').range)
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
area = DockArea()
win.setCentralWidget(area)
win.resize(1000,500)
win.setWindowTitle('pyqtgraph example: dockarea')
d1 = Dock("Dock1")
d2 = Dock("Dock2")
area.addDock(d1, 'bottom')
area.addDock(d2, 'bottom', d1)
w1 = pg.PlotWidget(title="Dock 1 plot")
it=w1.plot(np.random.normal(size=100))
d1.addWidget(w1)
w2 = pg.PlotWidget(title="Dock 2 plot")
w2.plot(np.random.normal(size=100))
d2.addWidget(w2)
w1.sigRangeChanged.connect(onSigRangeChanged)
w2.sigRangeChanged.connect(onSigRangeChanged)
win.show()
sys.exit(app.exec_())
Better yet,
Instead of disconnecting then reconnecting signals, it is possible to use blockSignals.
here is a generic way to synchronize any number of plots :
syncedPlots = [w1, w2, w3] # put as many plots as you wish
def onSigRangeChanged(r):
for g in syncedPlots:
if g !=r :
g.blockSignals(True)
g.setRange(xRange=r.getAxis('bottom').range)
g.blockSignals(False)
for g in syncedPlots:
g.sigRangeChanged.connect(onSigRangeChanged)
There is a better answer in this question:
Instead of connecting to the sigRangeChanged event we can directly link the axes
scales by w2.setXLink(w1).
Here is an attempt of doing a waterfall representation.
I need to express the signal(array) values(amplitudes/levels/densities) in different colors not in shades as done.
As I'm an algorithmic and signal processing eng. and not a software developer, I'm not familiar with the color maps and these stuff. So if someone could hep me out with the code for relating the colors with the signal values.
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import scipy.ndimage as ndi
import numpy as np
Nf = 90 # No. of frames
Ns = 100 # Signal length
app = QtGui.QApplication([])
w_SA = QtGui.QWidget(); w_SA.setFixedSize(400, 400)
# Create a GL View widget to display data
plt_SA1 = gl.GLViewWidget(w_SA); plt_SA1.move(10, 10); plt_SA1.resize(380, 380)
plt_SA1.setCameraPosition(elevation=90.0, azimuth=0.0, distance=70)
p1 = gl.GLSurfacePlotItem(shader='shaded', color=(0.5, 0.5, 1, 1), smooth=False)
p1.translate(-Nf/2, -Ns/2, 0)
plt_SA1.addItem(p1)
Arx = np.zeros([Nf, Ns])
def update():
global Arx
Arx = np.roll(Arx, 1, axis=0)
Arx[0] = ndi.gaussian_filter(np.random.normal(size=(1,Ns)), (1,1))
p1.setData(z=Arx)
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(30)
w_SA.show()
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Can you be more specific about how you want the image to be colored? If you don't need OpenGL, here is a simpler solution using pyqtgraph.ImageView. You can right-click on the gradient bar on the right side to change the lookup table used to color the image. There are also a variety of ways to set this table manually, depending on the desired effect.
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import scipy.ndimage as ndi
import numpy as np
Nf = 90 # No. of frames
Ns = 100 # Signal length
app = QtGui.QApplication([])
Arx = np.zeros([Nf, Ns])
win = pg.image(Arx)
win.view.setAspectLocked()
def update():
global Arx
Arx = np.roll(Arx, 1, axis=0)
Arx[0] = ndi.gaussian_filter(np.random.normal(size=(1,Ns)), (1, 1))
win.setImage(Arx.T, autoRange=False)
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(30)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()