如何提高在pyqtgraph中绘制大量TextItem的速度

how to improve the speed of drawing a lot of TextItem in pyqtgraph

我正在尝试在 pyqtgraph 中标记一些位置,

这是代码。

   def set_history_arrow(self):
        print(f"===> up: {len(self.up)} down: {len(self.down)}")
        for up in self.up:
            print("up", self.up.index(up))
            up_arrow = pg.TextItem('↑', fill=(255, 0, 0))
            self.plot_widget.addItem(up_arrow)
            up_arrow.setPos(up, self.data[up])
        for down in self.down:
            print("down", self.down.index(down))
            down_arrow = pg.TextItem("↓", fill=(0, 238, 118))
            self.plot_widget.addItem(down_arrow)
            down_arrow.setPos(down, self.data[down])
    

数据总和为:41363

他们的数量,

===> up: 4259 down: 2559

执行起来很慢

如何提高速度?

只需使用 ScatterPlotItem 就可以了

如果您有大量文本,您可能会受益于使用 QPainter.drawText http://doc.qt.io/qt-5/qpainter.html#drawText

更直接地渲染文本

PyQtGraph中有一个例子可以在这里绘制自定义图形:https://github.com/pyqtgraph/pyqtgraph/blob/master/examples/customGraphicsItem.py

虽然该示例并未显示如何绘制文本,但它本身并不简单。

这要复杂得多,@somewheve 的建议可能更容易实施。