【问题标题】:Scrolling X-axis with pyqt5使用 pyqt5 滚动 X 轴
【发布时间】:2018-04-19 08:25:33
【问题描述】:

目前,我正在开发一个 GUI,该 GUI 具有来自 Arduino 的传入数据,并且正在被绘制。现在它正在根据最后 50 个样本进行绘制。我找到了一种方法来增加 X 轴的大小,但是我希望它随着数据的进入而改变,这样它就可以表示时间而不是已经看到的样本。我还想看看如何放置 x 轴标签。 matplotlib方法我试过了,还是不行。

class GUI(QWidget, Ui_DynoTest1):
    def __init__(self, parent=None, border = None):

        # This is what is used to make the graph.        
        QWidget.__init__(self, parent)
        self.setupUi(self)
        self.thread = GetData(self)
        self.thread.dataChanged.connect(self.onDataChanged)
        self.thread.start()
        self.torque = []
        self.horse_power = []

        layout = QHBoxLayout()
        self.plot = pg.PlotWidget()
        layout.addWidget(self.plot)
        self.plot.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
        self.graphicsView.setLayout(layout)

        self.p1 = self.plot.plotItem
        self.p1.setLabels(left='Torque')
        self.TorqueCurve = self.p1.plot()
        self.TorqueCurve.setPen(pg.mkPen(QColor(0,0,0), width=2.5))

        self.p2 = pg.ViewBox()
        self.HorsePowerCurve = pg.PlotCurveItem()
        self.HorsePowerCurve.setPen(pg.mkPen(QColor(0, 0, 255), width=2.5))
        self.p2.addItem(self.HorsePowerCurve)
        self.p1.scene().addItem(self.p2)
        self.p1.showAxis('right')
        self.p1.getAxis('right').setLabel('HorsePower', color='#0000ff')
        self.p1.getAxis('right').linkToView(self.p2)
        self.p1.vb.sigResized.connect(self.updateViews)
        if len(self.torque) < 50:
            self.torque.append(Torque)
        else:
            self.torque[:-1] = self.torque[1:] + [Torque]

        if len(self.horse_power) < 50:
            self.horse_power.append(HorsePower)
        else:
            self.horse_power[:-1] = self.horse_power[1:] + [HorsePower]

        self.TorqueCurve.setData(self.torque)
        self.HorsePowerCurve.setData(self.horse_power)
        self.updateViews()

【问题讨论】:

  • 据我了解,您希望 x 轴发生变化,例如:1、2、3、... 50,以及 2、3、4、... 51 和另一次之后的瞬间3,4,5, .. ., 52. 我是对的?
  • 您提供的代码没有定义pg。那是什么?
  • @PaulCornelius pg 是 pyqtgraph
  • @eyllanesc 你是对的,我喜欢框架大小,但我认为像图表那样有 x 轴移动会更好。

标签: python plot python-3.5 pyqt5 pyqtgraph


【解决方案1】:

如果您将 pyqtgraph 作为数据仅传递一个列表,这将放置 x 轴,但是如果我们传递两个轴,则使用我们的轴,我们必须创建一个为其提供时间的列表,我们使用一个变量来保存初始时间。

如果要为 x 放置标签,可以使用以下模式之一:

self.p1.setLabels(left='Torque', bottom='Time')
self.p1.setLabel('bottom', 'Time', units="samples")

在第一个中您可以指示所有标签,在第二个中是单独的。

class GUI(QWidget, Ui_DynoTest1):
    def __init__(self, parent=None):
        [...]
        self.torque = []
        self.horse_power = []
        self.time = []
        self.counter = 0
        [...]
        self.p1 = self.plot.plotItem
        self.p1.setLabels(left='Torque', bottom='Time')
        # self.p1.setLabel('bottom', 'Time', units="samples")
        [...]

    def onDataChanged(self, Force, RPM, Max_RPM, Torque, Max_Torque, HorsePower, Max_HorsePower, Run_Time):
        [...]
        if self.counter < 50:
            self.torque.append(Torque)
            self.horse_power.append(HorsePower)
            self.time.append(Run_Time)
        else:
            self.torque = self.torque[1:] + [Torque]
            self.horse_power = self.horse_power[1:] + [HorsePower]
            self.time = self.time[1:] + [Run_Time]
        self.HorsePowerCurve.setData(self.time, self.horse_power)
        self.TorqueCurve.setData(np.array(self.time), self.torque)
        self.updateViews()

【讨论】:

  • 通过将 If else 语句替换为以 t1= 开头的语句,图表不会更新。如果我保留 if else 语句并添加 t1= 它确实会滚动,但它仍然不符合时间流逝的时间。我知道它是基于我输入的样本数量,所以如果我降低采样率应该会减慢 x 轴,但我觉得必须有一种方法可以将 Run_Time 设置为 X -axis 或任何其他变量,例如 force 或 rpms。
  • @MatthewEspindolaMunn 不要替换它,只是添加到最后,我更新了我的答案。
  • 这是我首先尝试做的,但这就是导致 x 轴与时间不同的原因。它确实有滚动的 X 轴,但我希望我可以让 x 轴成为我希望它不仅仅是已经经历的样本的任何变量。
  • @MatthewEspindololaMunn 已经修改了我的答案,请检查我的答案
  • 这似乎非常有效!现在,如果我想将 X 轴更改为其他变量,我会用 self.variable 替换 .self.time 的所有内容,最后用我选择的任何变量替换 Run_Time
猜你喜欢
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
相关资源
最近更新 更多