Custom scatter plot symbols

1,120 views
Skip to first unread message

Mikhail Terekhov

unread,
Mar 21, 2014, 9:45:24 PM3/21/14
to pyqt...@googlegroups.com
Hi,

I'm trying to create custom symbols for a scatter plot from a text symbols but can't figure out  how to choose the correct font size. I'd like to create a symbols that do not change size when plot is zoomed in or out. What would be the right way to achieve that? Below is the example of what I've got so far.

Regards,
Mikhail

# -*- coding: utf-8 -*-
import initExample

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from pyqtgraph import functions as fn


app = QtGui.QApplication([])

mw = QtGui.QMainWindow()
view = pg.GraphicsLayoutWidget()  ## GraphicsView with GraphicsLayout inserted by default
mw.setCentralWidget(view)
mw.show()
mw.setWindowTitle('pyqtgraph example: ScatterPlot')
w = view.addPlot()

mysymbol = QtGui.QPainterPath()
mysymbol.addText(0.0, 0.0, QtGui.QFont("San Serif", 1), "Y")

s = pg.ScatterPlotItem(pxMode=True)   ## Set pxMode=False to allow spots to transform with the view
spots = []
for i in range(10):
    for j in range(10):
        spots.append({'pos': (i, j), 'size': 20, 'pen': fn.mkPen({'color': 'w', 'width': 2}),
        'brush':pg.intColor(i*10+j, 100), 'symbol': mysymbol})
s.addPoints(spots)
w.addItem(s)

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


Luke Campagnola

unread,
Mar 21, 2014, 10:36:01 PM3/21/14
to pyqt...@googlegroups.com
On Fri, Mar 21, 2014 at 9:45 PM, Mikhail Terekhov <ter...@gmail.com> wrote:
Hi,

I'm trying to create custom symbols for a scatter plot from a text symbols but can't figure out  how to choose the correct font size. I'd like to create a symbols that do not change size when plot is zoomed in or out. What would be the right way to achieve that?

Interesting! The important details are: 

1. The symbol path must fit entirely inside the box from (-0.5, -0.5) to (0.5, 0.5).
2. You can use path.boundingRect() to decide how to rescale the path to fit inside the unit box.

Example:

mysymbol = QtGui.QPainterPath()
mysymbol.addText(0, 0, QtGui.QFont("San Serif", 10), 'Y')
br = mysymbol.boundingRect()
scale = min(1. / br.width(), 1. / br.height())
tr = QtGui.QTransform()
tr.scale(scale, scale)
tr.translate(-br.x() - br.width()/2., -br.y() - br.height()/2.)
mysymbol = tr.map(mysymbol)


Luke

Mikhail Terekhov

unread,
Mar 22, 2014, 12:48:56 PM3/22/14
to pyqt...@googlegroups.com
Thank you Luke, it works perfectly!
Reply all
Reply to author
Forward
0 new messages