Search code examples
symbolsscatter-plotpyqtgraph

Symbol '|' in pyqtgraph


I am trying to make a scatter plot with '|' symbol (as of matplotlib) in pyqtgraph. This symbol is not available in pyqtgraph. Could anyone know what should I make to reproduce it?


Solution

  • There is indeed no "|" symbol in pyqtgraph.
    However, You can create Your own symbol from any character.
    Here is a short code with function, that creates new symbol from any character:

    import sys
    
    import numpy as np
    import pyqtgraph as pg
    from PyQt5 import QtGui
    from PyQt5.QtGui import QFont
    from PyQt5.QtWidgets import QApplication
    
    
    def custom_symbol(symbol: str, font: QFont = QtGui.QFont("San Serif")):
        """Create custom symbol with font"""
        # We just want one character here, comment out otherwise
        assert len(symbol) == 1
        pg_symbol = QtGui.QPainterPath()
        pg_symbol.addText(0, 0, font, symbol)
        # Scale symbol
        br = pg_symbol.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.)
        return tr.map(pg_symbol)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        x = np.random.normal(size=1000)
        y = np.random.normal(size=1000)
    
        pg.plot(x, y, pen=None, symbol=custom_symbol("|"))
    
        status = app.exec_()
        sys.exit(status)
    

    I've used older post to prepare this answer, that can be found here.