Pyqtgraph:重载的 AxisItem 仅显示原始数据
Pyqtgraph: Overloaded AxisItem only showing original data
我正在构建一个应用程序以根据滚动图示例查看实时数据。我的 x 轴应将时间显示为格式化字符串。添加到图中的 x 值是以秒为单位的时间戳浮点数。这是我的 plot window 代码的简化版本。
一切都是实时工作的,我可以毫无问题地显示我想要绘制的值,但是我的 x 轴的标签只是时间戳,而不是格式化的字符串。我知道 AxisItem 中的函数 formatTimestampToString(val) 过载 returns 一个好的字符串值。
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:我可以毫无问题地添加和减去标签,但它只会在添加标签时更新图例框的大小。这意味着当我在情节中添加和删除 curves/data 时,图例会增长,但不会再缩小。我尝试在删除标签后调用 LegendItem.updateSize() 函数,但没有任何反应。
希望对您有所帮助!谢谢
所以我发现了问题。在 self.plot = self.plotWidget.addPlot(axisItem={'bottom': axis})
中,它应该说的是 axisItems,而不是 axisItem。
我正在构建一个应用程序以根据滚动图示例查看实时数据。我的 x 轴应将时间显示为格式化字符串。添加到图中的 x 值是以秒为单位的时间戳浮点数。这是我的 plot window 代码的简化版本。
一切都是实时工作的,我可以毫无问题地显示我想要绘制的值,但是我的 x 轴的标签只是时间戳,而不是格式化的字符串。我知道 AxisItem 中的函数 formatTimestampToString(val) 过载 returns 一个好的字符串值。
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:我可以毫无问题地添加和减去标签,但它只会在添加标签时更新图例框的大小。这意味着当我在情节中添加和删除 curves/data 时,图例会增长,但不会再缩小。我尝试在删除标签后调用 LegendItem.updateSize() 函数,但没有任何反应。
希望对您有所帮助!谢谢
所以我发现了问题。在 self.plot = self.plotWidget.addPlot(axisItem={'bottom': axis})
中,它应该说的是 axisItems,而不是 axisItem。