python - 画半无限线?

标签 python pyqt pyqtgraph

我使用 pyqtgraph 进行数据采集,我必须在图形 View 上表示一些阈值。例如表示高压限制等。 我使用了 pyqtgraph 中的 InfiniteLine 类,但现在,我必须考虑在采集过程中阈值的一些可能变化。它看起来像是两条无限线之间的一步(请查找所附示例)。

example

为此,我必须画一条半无限线。你知道一个简单的方法吗?

我想过使用一些受 viewBox 最小值和最大值限制的 plotCurveItem :

thresholdValue = 60V # just an example
range = self.viewBox.viewRange()
xRange = range[0]   # we only want ViewBox horizontal limits
minView = xRange[0]
maxView = xRange[1]
myPlotCurveItem = pyqtgraph.PlotCurveItem([minView, maxView],[thresholdValue, thresholdValue])

如果改变阈值:

newThresholdValue = 70V

plotCurveItem 的 x 数据将变为:

[minView, changingTime]    #with changinTime : the moment we change the threshold

然后我们将添加一个新的 plotCurveItem :

myNewPlotCurveItem = pyqtgraph.plotCurveItem([changingTime, maxView],[newThresholdValue, newThresholdValue])

这个解决方案看起来不错吗?或者您认为它有什么问题吗?

最佳答案

您的方法看起来不错,并且主要是 pyqtgraph.InfiniteLine 正在做的事情。我检查了the source of InfiniteLine并提取那些绝对必要的部分并添加变化点和二级信息,然后绘制三条线(左边界到左层变化点,变化点到右层右边界,两者的连接)。

完整代码如下:

from pyqtgraph.Qt import QtGui
import numpy as np
import pyqtgraph as pg

class InfiniteLineWithBreak(pg.GraphicsObject):

    def __init__(self, changeX, levelsY, pen=None):
        pg.GraphicsObject.__init__(self)

        self.changeX = changeX
        self.levelsY = levelsY

        self.maxRange = [None, None]
        self.moving = False
        self.movable = False
        self.mouseHovering = False

        pen = (200, 200, 100)
        self.setPen(pen)
        self.setHoverPen(color=(255,0,0), width=self.pen.width())
        self.currentPen = self.pen


    def setBounds(self, bounds):
        self.maxRange = bounds
        self.setValue(self.value())

    def setPen(self, *args, **kwargs):
        self.pen = pg.fn.mkPen(*args, **kwargs)
        if not self.mouseHovering:
            self.currentPen = self.pen
            self.update()

    def setHoverPen(self, *args, **kwargs):
        self.hoverPen = pg.fn.mkPen(*args, **kwargs)
        if self.mouseHovering:
            self.currentPen = self.hoverPen
            self.update()

    def boundingRect(self):
        br = self.viewRect()
        return br.normalized()

    def paint(self, p, *args):
        br = self.boundingRect()
        p.setPen(self.currentPen)
        # three lines (left border to change point, change point vertical, change point to right)
        p.drawLine(pg.Point(br.left(), self.levelsY[0]), pg.Point(self.changeX, self.levelsY[0]))
        p.drawLine(pg.Point(self.changeX, self.levelsY[0]), pg.Point(self.changeX, self.levelsY[1]))
        p.drawLine(pg.Point(self.changeX, self.levelsY[1]), pg.Point(br.right(), self.levelsY[1]))

    def dataBounds(self, axis, frac=1.0, orthoRange=None):
        if axis == 0:
            return None   ## x axis should never be auto-scaled
        else:
            return (0,0)

    def setMouseHover(self, hover):
        pass

app = QtGui.QApplication([])
w = pg.GraphicsWindow()
w.resize(1000, 600)
v = w.addPlot(y=np.random.normal(size=100))
v.addItem(InfiniteLineWithBreak(changeX=50, levelsY=(-1, 1)))
app.exec_()

看起来像:

enter image description here

可以添加的是对悬停和使用鼠标更改值(更改点和级别)甚至旋转 90 度的 react 。 InfiniteLine 是如何做到这一点的一个很好的例子。

关于python - 画半无限线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702642/

相关文章:

python - win7上pyqt中的PyQt QIcon.setThemeSearchPaths()输入什么路径?

python - 在 Pyqtgraph 中绘制交互式时间跟踪器

python - 如何在pyqtgraph中设置绘制距离?

python - 语言检测

python - Django - 使用 ManyToMany 保存表单集

python - 如何更改我的 Django 服务器时间

python将数字添加到字符串

qt - 如何在缩放时支持qgraphicsproxywidget中的高分辨率像素图

python - 添加 QTextEdit 小部件作为中央小部件使主窗口不弹出,并且 showMaximize 导致计算机崩溃?

python - 如果 pyqtgraph 在 GUI 中,则它没有交互模式