Re: [pyqtgraph] how to add text or label in Pyqtgraph 3D

3,715 views
Skip to first unread message

Luke Campagnola

unread,
May 19, 2014, 2:55:37 PM5/19/14
to pyqt...@googlegroups.com
On Fri, May 16, 2014 at 4:53 PM, yongqinchuan du <duqc...@gmail.com> wrote:
I wondering if there is a way to add text or Scale into 3D plot?  Something like this in 2d plots below.

Hi,
There is not currently any text support in PyQtGraph's 3D graphics. There are a few options that you could try:
  - Render text to an image (using QPainter.drawText) and then display the image in the view
  - http://qt-project.org/doc/qt-4.8/qglwidget.html#renderText

Please note that this system will hopefully be replaced soon with VisPy, so I am generally not adding new features to it until that time.

Miha Pirnat

unread,
Sep 4, 2014, 3:05:03 PM9/4/14
to pyqt...@googlegroups.com
Hello,

would it be possible to use renderText in this way:

import pyqtgraph.opengl as gl
import pyqtgraph as pg
import PySide.QtCore as pqc
import PySide.QtGui as pqg

app = pqg.QApplication([])
w = gl.GLViewWidget()

w.show()

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

w.qglColor(pqc.Qt.white)
w.renderText(0.,0.,0.,'text')

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


The above code runs, but text is not shown. I'm obviously doing something very wrong here as I am very new to pyQtgraph and also OpenGL.

Thank you for any help. 

Luke Campagnola

unread,
Sep 4, 2014, 3:32:11 PM9/4/14
to pyqt...@googlegroups.com
On Thu, Sep 4, 2014 at 3:05 PM, Miha Pirnat <miha.pi...@gmail.com> wrote:
Hello,

would it be possible to use renderText in this way:

import pyqtgraph.opengl as gl
import pyqtgraph as pg
import PySide.QtCore as pqc
import PySide.QtGui as pqg

app = pqg.QApplication([])
w = gl.GLViewWidget()

w.show()

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

w.qglColor(pqc.Qt.white)
w.renderText(0.,0.,0.,'text')

This should work if you make a subclass of GLViewWidget that calls renderText from inside its paintGL method:

class MyGLView(GLViewWidget):
    def paintGL(self, *args, **kwds):
        GLViewWidget.paintGL(self, *args, **kwds)
        self.qglColor(Qt.white)
        self.renderText(0, 0, 0, 'text')

Miha Pirnat

unread,
Sep 4, 2014, 4:53:01 PM9/4/14
to pyqt...@googlegroups.com
It works :). Thank you.

Igor Shovkun

unread,
Dec 17, 2014, 1:41:38 PM12/17/14
to pyqt...@googlegroups.com
Luke, could you just give one working example of how this rendering should look like?

Muneeb Imran

unread,
Jun 2, 2016, 5:53:21 AM6/2/16
to pyqtgraph
Hello Luke,

Could you please tell me the format or datatype used for qglColor. I am having issue passing any color. Or could you may be still have this working file, It would be helpful.

Maher Albezem

unread,
Aug 16, 2019, 7:16:58 AM8/16/19
to pyqtgraph
from PyQt5 import Qt
from PyQt5.QtGui import QColor
from pyqtgraph.opengl import GLViewWidget


class GLView(GLViewWidget):
"""
I have implemented my own GLViewWidget
"""
def __init__(self, parent=None):
super().__init__(parent)

def mouseReleaseEvent(self, ev):
region = (ev.pos().x() - 5, ev.pos().y() - 5, 10, 10)
print(self.itemsAt(region))


def paintGL(self, *args, **kwds):
GLViewWidget.paintGL(self, *args, **kwds)
        self.qglColor(QColor("k"))

self.renderText(0, 0, 0, 'text')

Nick PV

unread,
Mar 29, 2020, 10:54:30 PM3/29/20
to pyqtgraph
I'm looking for the same thing (text / label rendering at 3D space) but I'm sorry it's not enough information here (in terms of implementation) to understand how to actually implement this feature.

For example I have this (one line plotting at 3D space), how to add a label near by it. 

code sample: 

import numpy as np
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtWidgets
from pyqtgraph import Vector


class plot3D(object):
def __init__(self):
self.line_input = 0
self.app = QtWidgets.QApplication(sys.argv)
self.w = gl.GLViewWidget()
        axis = gl.GLAxisItem()
self.w.addItem(axis)

    def plot_line(self):
        pl_line = np.array([(338.12, 508.03, 0.0), (338.12, 0.0, 0.0)])
color = (250, 0, 0, 0.7)
newline = gl.GLLinePlotItem(pos=pl_line, color=color, width=2, antialias=False)
self.w.addItem(newline)
self.w.show()

    def exec(self):
self.app.exec()


pl3d = plot3D()
pl3d.plot_line()
pl3d.exec()


Reply all
Reply to author
Forward
0 new messages