Text in 3d plot

1,577 views
Skip to first unread message

ronan

unread,
Feb 2, 2016, 8:43:05 AM2/2/16
to pyqtgraph
Hi

Is it possible to put text in a 3d plot ising PyQtGraph?

I would like to place various markers in a 3d scene and to label these using short strings visible to the user. Is there a way to do this?

I looked at the textItem documentation here http://pyqtgraph.org/documentation/graphicsItems/textitem.html but the anchor seems to be in 2d "widget space" and not in the 3d scene. Is there a way to give the text a 3d anchor?


Thanks


Ronan



Ludovic Angot

unread,
May 30, 2016, 2:08:52 AM5/30/16
to pyqtgraph
The only hint I can provide is from here:
https://groups.google.com/d/msg/pyqtgraph/FNxcMh3M6nc/XGS2C447n_MJ

It works for me but I don't know how to add multiple text instances, if you or any one knows, thanks to help!

Here is a simple working example based on the above:
import pyqtgraph.opengl as gl
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore as pqc
from pyqtgraph.Qt import QtGui as pqg

class MyGLView(gl.GLViewWidget):
    def __init__(self, X,Y,Z,text):
        super(MyGLView, self).__init__()
        self.text = text
        self.X = X
        self.Y = Y
        self.Z = Z

    def setText(self, text):
        self.text = text
        self.update()

    def setX(self, X):
        self.X = X
        self.update()

    def setY(self, Y):
        self.Y = Y
        self.update()
       
    def setZ(self, Z):
        self.Z = Z
        self.update()       
       
    def paintGL(self, *args, **kwds):
        gl.GLViewWidget.paintGL(self, *args, **kwds)
        self.qglColor(Qt.white)
        self.renderText(self.X, self.Y, self.Z, self.text)

app = pqg.QApplication([])
w = MyGLView(X=0,Y=5,Z=0,text="Your text")

g = gl.GLGridItem()
w.addItem(g)

w.show()

if __name__ == '__main__':
    pqg.QApplication.instance().exec_()

Ludovic

Xinfa Zhu

unread,
Jan 29, 2018, 1:05:07 AM1/29/18
to pyqtgraph
Thank you Ludovic for the helpful example. I came to this need and made it an opengl item, which is similar to other existing items and fit in my project better. Then I put it into my fork of the pyqtgraph GitHub repo; I will see to make a PR of it if it can help others.

import pyqtgraph.opengl as gl
from pyqtgraph.opengl.GLGraphicsItem import GLGraphicsItem
from pyqtgraph.Qt import QtCore, QtGui

class GLTextItem(GLGraphicsItem):
    def __init__(self, X=None, Y=None, Z=None, text=None):
        GLGraphicsItem.__init__(self)

        self.text = text
        self.X = X
        self.Y = Y
        self.Z = Z

    def setGLViewWidget(self, GLViewWidget):
        self.GLViewWidget = GLViewWidget

    def setText(self, text):
        self.text = text
        self.update()

    def setX(self, X):
        self.X = X
        self.update()

    def setY(self, Y):
        self.Y = Y
        self.update()

    def setZ(self, Z):
        self.Z = Z
        self.update()

    def paint(self):
        self.GLViewWidget.qglColor(QtCore.Qt.white)
        self.GLViewWidget.renderText(self.X, self.Y, self.Z, self.text)

def main():
    app = QtGui.QApplication([])
    w = gl.GLViewWidget()
    w.opts['distance'] = 40
    w.show()

    t = GLTextItem(X=0, Y=5, Z=10, text="Your text")
    t.setGLViewWidget(w)
    w.addItem(t)

    g = gl.GLGridItem()
    w.addItem(g)

    app.exec_()

if __name__ == '__main__':
    main()

Nick

unread,
Dec 24, 2020, 1:08:47 PM12/24/20
to pyqtgraph
I noticed that this object cannot generate text that spans in multiple lines. 
For example, if I try to render "Line #1\r\nLine#2", everything gets rendered in the same line. 
Is there a workaround for that ? 
Reply all
Reply to author
Forward
0 new messages