【问题标题】:Pyqtgraph: Overloaded AxisItem only showing original dataPyqtgraph:重载 AxisItem 仅显示原始数据
【发布时间】:2019-04-10 21:48:54
【问题描述】:

我正在构建一个应用程序来查看基于滚动图示例的实时数据。我的 x 轴应将时间显示为格式化字符串。添加到图中的 x 值是以秒为单位的时间戳浮动。这是我的绘图窗口代码的简化版本。

一切都是实时工作的,我可以毫无问题地显示我想要绘制的值,但我的 x 轴的标签只是时间戳而不是格式化的字符串。我知道 AxisItem 重载中的函数 formatTimestampToString(val) 返回一个很好的字符串值。

import pyqtgraph as pg


class NewLegend(pg.LegendItem):
    def __init__(self, size=None, offset=None):
        pg.LegendItem.__init__(self, size, offset)

    def paint(self, p, *args):
        p.setPen(pg.mkPen(0,0,0)) # outline
        p.setBrush(pg.mkBrush(255,255,255)) # background
        p.drawRect(self.boundingRect())


class DateAxis(pg.AxisItem):
    def tickStrings(self, values, scale, spacing):
        strings = []
        for val in values:
             strings.append(formatTimestampToString(val))
        return strings


class PlotWindow(QtWidgets.QWidget):

    def __init__(self, iof, num):
        QtWidgets.QWidget.__init__(self)
        self.setWindowTitle('Plot Window ')
        self.resize(1000, 800)

        pg.setConfigOption('background', 'w')
        pg.setConfigOption('foreground', 'k')

        """
        ... other stuff ...
        """

        # Externally updated dict with data
        self.data = {}

        self.curves = {}

        self.plotWidget = pg.GraphicsWindow("Graph Window")

        axis = DateAxis(orientation='bottom')
        self.plot = self.plotWidget.addPlot(axisItem={'bottom': axis})
        self.plot.setAutoPan(x=True)
        self.legend = NewLegend(size=(100, 60), offset=(70, 30))
        self.legend.setParentItem(self.plot.graphicsItem())

    def updatePlots(self):
        #update data for the different curves
        for data_name, curve in self.curves.items():
            if curve != 0:
                curve.setData(y=self.data[data_name].values[:], x=self.data[data_name].timestamps[:])

    def addCurve(self, data_name):
        if data_name not in self.curves:
                self.curves[data_name] = self.plot.plot(y=self.data[data_name].values[:], x=self.data[data_name].timestamps[:], pen=pg.mkPen(color=self.randomColor(),width=3))
                self.legend.addItem(self.curves[data_name], name=data_name)

    def removeCurve(self, data_name):
        if data_name in self.curves:
            self.plot.removeItem(self.curves[data_name])
            self.legend.removeItem(data_name)
            del self.curves[data_name]

我做错了吗?有没有更好的方法来重载 AxisItem?我还尝试用一个简单的数字公式重载 AxisItem,看看它是否对我的轴有任何影响。

我遇到的另一个问题是 LegendItem:我可以毫无问题地添加和减去标签,但它只会在添加标签时更新图例框的大小。这意味着当我在绘图中添加和删除曲线/数据时,图例会增长,但不会再次缩小。我尝试在删除标签后调用 LegendItem.updateSize() 函数,但没有任何反应。

希望你能帮上忙!谢谢

【问题讨论】:

    标签: pyqt python-3.6 pyqtgraph


    【解决方案1】:

    所以我发现了问题。在self.plot = self.plotWidget.addPlot(axisItem={'bottom': axis}) 它应该说axisItems,而不是axisItem。

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 2015-06-16
      • 2021-10-20
      • 2020-04-03
      • 1970-01-01
      • 2020-05-20
      • 2022-01-02
      • 2020-01-02
      相关资源
      最近更新 更多