如何在pyqtgraph中设置跟踪名称?

2024-04-20 05:58:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我用这个类来画一个跟踪,我有两条线要跟踪,但我不能显示每一条线的名称,如何?你知道吗

class Plot2D():
    def __init__(self):
        self.traces = dict()

        #QtGui.QApplication.setGraphicsSystem('raster')
        self.app = QtGui.QApplication([])
        #mw = QtGui.QMainWindow()
        #mw.resize(800,800)

        self.win = pg.GraphicsWindow(title="Detecting cluck")
        self.win.resize(1000,600)
        self.win.setWindowTitle('Detecting')
        # Enable antialiasing for prettier plots
        pg.setConfigOptions(antialias=True)

        self.canvas = self.win.addPlot(title="改装车检测")
        self.canvas.setYRange(0, 1)

    def start(self):
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

    def trace(self,name,dataset_x,dataset_y,sColor):
        if name in self.traces:
            self.traces[name].setData(dataset_x,dataset_y)
        else:
            self.traces[name] = self.canvas.plot(
                pen=pg.mkPen(sColor, width=3), name="car")

我得到的是:

我得到了什么

我想要的是:

enter image description here


Tags: nameselfiftitledefwindatasetmw
1条回答
网友
1楼 · 发布于 2024-04-20 05:58:29

除了在绘图中建立名称外,还必须使用addLegend()

import sys
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np

class Plot2D():
    def __init__(self):
        self.traces = dict()

        self.app = QtGui.QApplication([])

        self.win = pg.GraphicsWindow(title="Detecting")
        self.win.resize(1000,600)
        pg.setConfigOptions(antialias=True)

        self.canvas = self.win.addPlot(title="改装车检测")
        self.canvas.addLegend()
        self.canvas.setYRange(0, 1)

    def start(self):
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

    def trace(self,name,dataset_x,dataset_y,sColor):
        if name in self.traces:
            self.traces[name].setData(dataset_x,dataset_y)
        else:
            self.traces[name] = self.canvas.plot(dataset_x, dataset_y,
                pen=pg.mkPen(sColor, width=3), name=name)

if __name__ == '__main__':
    p = Plot2D()
    p.trace("name1", range(100), 0.5 + np.random.normal(size=100, scale=0.1), 'r')
    p.trace("name2", range(100), 0.5 + np.random.normal(size=100, scale=0.1), 'w')
    p.start()

enter image description here

更新:

如果要更改字体大小,可以使用HTML

self.traces[name] = self.canvas.plot(dataset_x, dataset_y,
    pen=pg.mkPen(sColor, width=3), name='''<font size="15">{}</font>'''.format(name))

enter image description here

相关问题 更多 >