如何将自定义AxisItem添加到现有PlotWidget?

问题描述 投票:3回答:3

我正在尝试将pyqtgraph中的自定义AxisItem添加到由Qt Designer生成的现有PlotWidget中。有相关的主题here,但是代码示例没有确切的答案,我无法发表评论,所以我创建了一个新主题。

这是我的自定义AxisItem(基于this代码):

import pyqtgraph as pg
import datetime

def int2td(ts):
    return(datetime.timedelta(seconds=float(ts)/1e6))

class TimeAxisItem(pg.AxisItem):
    def __init__(self, *args, **kwargs):
        super(TimeAxisItem, self).__init__(*args, **kwargs)
    def tickStrings(self, values, scale, spacing):
        return [int2dt(value).strftime("%H:%M:%S") for value in values]

这是我的主要QtPlotter类:

from pyqtgraph.Qt import QtGui
from template_pyqt import Ui_Form # Ui_Form is generated by Qt Designer

class QtPlotter:
    def __init__(self):
        self.app = QtGui.QApplication([])
        self.win = QtGui.QWidget()
        self.ui = Ui_Form()
        self.ui.setupUi(self.win)
        self.win.show()

        self.ui_plot = self.ui.plot 
        self.ui_plot.showGrid(x=True, y=True)

然后我试图添加我的自定义AxisItem:

self.ui_plot.getPlotItem().axes['bottom']['item'] = TimeAxisItem(orientation='bottom')

我没有错误,但这没有任何效果。

python python-2.7 pyqt pyqt4 pyqtgraph
3个回答
2
投票

PlotItem类没有setAxis方法,只有getAxis方法来获取当前轴。您可以在axisItems构造函数的PlotItem参数中指定包含轴项的字典,但似乎无法更新现有AxisItemPlotItem

即使PlotItem.axes字典是公共属性,它也没有文档,因此使用它有点冒险。 PyQtGraph的作者可能会改变它的行为,或者重命名它(虽然这种情况的变化很小)。恕我直言,它应该是一个私人属性。

在任何情况下,通过查看source构造函数的PlotItem,您可以看到如何将axisItem参数中的轴项添加到绘图项:

    ## Create and place axis items
    if axisItems is None:
        axisItems = {}
    self.axes = {}
    for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))):
        if k in axisItems:
            axis = axisItems[k]
        else:
            axis = AxisItem(orientation=k, parent=self)
        axis.linkToView(self.vb)
        self.axes[k] = {'item': axis, 'pos': pos}
        self.layout.addItem(axis, *pos)
        axis.setZValue(-1000)
        axis.setFlag(axis.ItemNegativeZStacksBehindParent)

也许你可以通过查看上面的代码使它工作。再次,这是无证行为,使用风险自负!此外,在设置新的AxisItem以防止内存泄漏时,应正确删除和取消链接旧的PlotItem.setAxis。也许这很棘手,这可能是source方法不存在的原因。


0
投票

我查看了PlotItem构造函数的here一整天,但我无法让它工作。

最后,我发现QtDesigner / QtCreator只在PlotWidget上输出三行。而不是尝试将TimeAxisItem添加到现有的PlotWidget,只需删除现有的PlotWidget,然后使用TimeAxisItem创建一个新的PlotWidget,就像from PyQt5 import QtCore import pyqtgraph as pg parent = self.ui_plot.parent() geom_object = self.ui_plot.frameGeometry() geometry = QtCore.QRect(geom_object.left(), geom_object.top(), geom_object.width(), geom_object.height()) object_name = self.ui_plot.objectName() del self.ui_plot time_axis = TimeAxisItem(orientation='bottom') self.ui_plot = pg.PlotWidget(parent, axisItems={'bottom': time_axis}) self.ui_plot.setGeometry(geometry) self.ui_plot.setObjectName(object_name) 所描述的那样,更容易(但绝对不是更好)。

pyuic4 template.ui -o template.py 

match="from PyQt4 import QtCore, QtGui"
insert_class="from timeaxisitem_class import TimeAxisItem"
match_widget="self.plot = PlotWidget(Form)"
insert_timeaxis="self.plot = PlotWidget(Form, axisItems={'bottom': TimeAxisItem(orientation='bottom')})"

file="template.py"
sed -i "s/$match/$match\n$insert_class/" $file
sed -i "s/$match_widget/$insert_timeaxis/" $file

0
投票

既然有人试图在这里找到一些东西,我会发布我简单但不优雅的解决方法。

在将我的Qt Designer生成的模板转换为pyuic的python模板文件之后,我通过替换相应的PlotWidget将我的自定义AxisItem直接添加到python模板文件中。这一切都可以使用简单的bash脚本完成:

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.