- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从图中选择并获取两个图形的数据,我找到了一个示例,其中创建了一个自定义 View 框,然后创建一个像 pw = pg.PlotWidget(viewBox=vb) 这样的图,但是我不这样做当我们将情节作为 Qt Creator 推广的 pyqtgraph 小部件时知道该怎么做,下面是我在论坛中找到的示例(链接:https://groups.google.com/forum/#!topic/pyqtgraph/pTrem1RCKSw)这是代码(完美运行)
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Point import Point
from pyqtgraph.graphicsItems.ItemGroup import ItemGroup
from pyqtgraph.Qt import QtGui, QtCore
from matplotlib.mlab import inside_poly
from pyqtgraph.graphicsItems import ScatterPlotItem
class ChildGroup(ItemGroup):
sigItemsChanged = QtCore.Signal()
def __init__(self, parent):
ItemGroup.__init__(self, parent)
# excempt from telling view when transform changes
self._GraphicsObject__inform_view_on_change = False
def itemChange(self, change, value):
ret = ItemGroup.itemChange(self, change, value)
if change == self.ItemChildAddedChange or change == self.ItemChildRemovedChange:
self.sigItemsChanged.emit()
return ret
class MyViewBox(pg.ViewBox):
def mouseDragEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
ev.ignore()
else:
pg.ViewBox.mouseDragEvent(self, ev)
ev.accept()
pos = ev.pos()
if ev.button() == QtCore.Qt.RightButton:
if ev.isFinish():
self.rbScaleBox.hide()
self.ax = QtCore.QRectF(Point(ev.buttonDownPos(ev.button())), Point(pos))
self.ax = self.childGroup.mapRectFromParent(self.ax)
self.Coords = self.ax.getCoords()
self.getdataInRect()
self.changePointsColors()
else:
self.updateScaleBox(ev.buttonDownPos(), ev.pos())
def getdataInRect(self):
# Get the data from the Graphicsitem
self.getDataItem()
x = self.dataxy[0]
y = self.dataxy[1]
# Rect Edges
Xbl = (self.Coords[0],self.Coords[1]) # bottom left
Xbr = (self.Coords[2],self.Coords[1]) # bottom right
Xtr = (self.Coords[2],self.Coords[3]) # top right
Xtl = (self.Coords[0],self.Coords[3]) # top left
#Make a list of [(x0,y0),(x1,y1) ...]
self.xy = list()
for i in x:
tmp = (x[i],y[i])
self.xy.append(tmp)
self.insideIndex = inside_poly(self.xy,[Xbl, Xbr, Xtr, Xtl])
def getDataItem(self):
self.ObjItemList = pg.GraphicsScene.items(self.scene(),self.ax)
self.dataxy = self.ObjItemList[0].listDataItems()[0].getData()
def changePointsColors(self):
print(self.xy)
print(self.insideIndex)
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
mw.resize(800,500)
mw.show()
vb = MyViewBox()
pw = pg.PlotWidget(viewBox=vb)
a = np.array([0,1,2,3,4,5,6,7,8,9,10])
b = np.array([0,1,2,3,4,5,6,7,8,9,10])
curve0 = pw.plot(a,b, clickable=True, symbol = '+')
mw.setCentralWidget(pw)
# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
如何将之前的代码改编为 PlotWidget“Plot1”?
class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
pg.setConfigOption('background', 'w')
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.Plot1.plotItem.showGrid(True, True, 0.7)
self.Plot1.setLabel('left', 'q1')
self.Plot1.setLabel('bottom', 'Time', units='s')
self.Plot1.setTitle(title='q1')
pen = pg.mkPen({'color': "FF0000", 'width': 2})
pen1 = pg.mkPen({'color': "00FF00", 'width': 2})
self.Plot1.addLegend()
self.q1z = self.Plot1.plot(pen = pen, name = 'Trace 1')
self.q1k = self.Plot1.plot(pen = pen1, name = 'Trace 2')
这里是文件Sample.ui
有人可以帮助我吗?
最佳答案
解决方案是创建一个继承自 PlotWidget 的类,并具有一个到 MyViewBox 的 View 框,然后提升该小部件:
plotwidget.py
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
try:
from matplotlib.mlab import inside_poly
except ImportError:
# https://matplotlib.org/3.1.0/api/api_changes.html
# mlab.inside_poly has been removed in matplotlib 3.1.0
from matplotlib.path import Path
def inside_poly(points, verts):
poly = Path(verts)
return [idx for idx, p in enumerate(points) if poly.contains_point(p)]
class ChildGroup(pg.ItemGroup):
sigItemsChanged = QtCore.Signal()
def __init__(self, parent):
super(ChildGroup, self).__init__(parent)
# excempt from telling view when transform changes
self._GraphicsObject__inform_view_on_change = False
def itemChange(self, change, value):
ret = ItemGroup.itemChange(self, change, value)
if change == self.ItemChildAddedChange or change == self.ItemChildRemovedChange:
self.sigItemsChanged.emit()
return ret
class MyViewBox(pg.ViewBox):
def mouseDragEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
ev.ignore()
else:
pg.ViewBox.mouseDragEvent(self, ev)
ev.accept()
pos = ev.pos()
if ev.button() == QtCore.Qt.RightButton:
if ev.isFinish():
self.rbScaleBox.hide()
self.ax = QtCore.QRectF(
pg.Point(ev.buttonDownPos(ev.button())), pg.Point(pos)
)
self.ax = self.childGroup.mapRectFromParent(self.ax)
self.Coords = self.ax.getCoords()
self.getdataInRect()
self.changePointsColors()
else:
self.updateScaleBox(ev.buttonDownPos(), ev.pos())
def getdataInRect(self):
# Get the data from the Graphicsitem
self.getDataItem()
x = self.dataxy[0]
y = self.dataxy[1]
# Rect Edges
Xbl = (self.Coords[0], self.Coords[1]) # bottom left
Xbr = (self.Coords[2], self.Coords[1]) # bottom right
Xtr = (self.Coords[2], self.Coords[3]) # top right
Xtl = (self.Coords[0], self.Coords[3]) # top left
# Make a list of [(x0,y0),(x1,y1) ...]
self.xy = list()
for i in x:
tmp = (x[i], y[i])
self.xy.append(tmp)
self.insideIndex = inside_poly(self.xy, [Xbl, Xbr, Xtr, Xtl])
def getDataItem(self):
self.ObjItemList = pg.GraphicsScene.items(self.scene(), self.ax)
self.dataxy = self.ObjItemList[0].listDataItems()[0].getData()
def changePointsColors(self):
print(self.xy)
print(self.insideIndex)
class MyPlotWidget(pg.PlotWidget):
def __init__(self, parent=None):
super(MyPlotWidget, self).__init__(parent, viewBox=MyViewBox())
示例.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>487</width>
<height>368</height>
</rect>
</property>
<property name="palette">
<palette>
<active>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>86</red>
<green>86</green>
<blue>86</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>86</red>
<green>86</green>
<blue>86</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>86</red>
<green>86</green>
<blue>86</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>86</red>
<green>86</green>
<blue>86</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>30</number>
</property>
<property name="topMargin">
<number>40</number>
</property>
<property name="rightMargin">
<number>40</number>
</property>
<property name="bottomMargin">
<number>30</number>
</property>
<item>
<widget class="MyPlotWidget" name="Plot1">
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="backgroundBrush">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MyPlotWidget</class>
<extends>QGraphicsView</extends>
<header>plotwidget</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
pyuic5 Sample.ui -o sample.py -x
main.py
from PyQt5 import QtWidgets
import pyqtgraph as pg
from sample import Ui_Dialog
class MyApp(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
pg.setConfigOption("background", "w")
self.setupUi(self)
self.Plot1.plotItem.showGrid(True, True, 0.7)
self.Plot1.setLabel("left", "q1")
self.Plot1.setLabel("bottom", "Time", units="s")
self.Plot1.setTitle(title="q1")
pen = pg.mkPen({"color": "FF0000", "width": 2})
pen1 = pg.mkPen({"color": "00FF00", "width": 2})
self.Plot1.addLegend()
self.q1z = self.Plot1.plot(pen=pen, name="Trace 1")
self.q1k = self.Plot1.plot(pen=pen1, name="Trace 2")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MyApp()
w.show()
sys.exit(app.exec_())
├── main.py
├── plotwidget.py
├── sample.py
└── Sample.ui
关于python - 如何修改QT设计器生成的现有PlotWidget的viewbox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59124048/
我正在尝试使用 PyQt 绘图小部件创建实时数据图。我读到 PyQt 是绘制实时图表的最佳选择,但到目前为止我还没有取得任何成功。 我尝试使用方法 followed here 绘制随机数据然而,这种方
我正在尝试构建一个桌面应用程序,该应用程序将在一些流数据到达时绘制图表。我希望能够打开多个窗口来监控不同的流。 在我开始关闭图表窗口之前,它工作得很好。 我遇到的问题是,关闭我的一个图表窗口经常会导致
我正在制作一个时间序列监控程序。 我想从 x 轴范围的一半开始更改绘图的颜色。对于 100 x 20 的绘图小部件,我想将最后 50 个数据点更改为另一种颜色。 每传递 xx 项数据时,如何绘制自定义
这是我的代码: import sys import numpy as np import pyqtgraph as pg from PyQt5.QtWidgets import QApplicatio
我正在尝试将 pyqtgraph 中的自定义 AxisItem 添加到由 Qt Designer 生成的现有 PlotWidget。有相关话题here ,但是没有代码示例的确切答案,我无法发表评论,所
我正在尝试将样式设置为 pyqtgraph.PlotWidget 绘图区域的轴。我正在使用: font = QFont() font.setPointSize(14) font.setBold(Tru
我在使用 PySide 和 Qt Designer 制作的 GUI 方面需要一些帮助。在我的 Gui 代码中,我有 PlotWidget,我想在其中实现 pyqtgraph。 我正在尝试实现的 PyQ
作为问题的延续,我已经解决了 here几天前,我有一个 PyQt4 GUI,它嵌入了两个 PyQtGraph 的 PlotWidgets,每个 PlotWidgets 从一个按钮触发的线程随机附加数组
我在网格中放置了一些小部件。但这些小部件始终存在奇怪的行为。 如果将一个 ImageView 和 PlotWidget 放在附近,ImageView 会收缩,PlotWidged 会拉伸(stretc
我正在尝试制作一个应用程序,我想在其中使用多个 PlotWidgets 来绘制来 self 的 Arduino 中最多 5 个传感器的信号。一旦我有两个更新图,GUI 就没有响应,我需要暂停/重新开始
如何使 x 轴刻度加粗? plot_1 = pg.PlotWidget() plot_1.setBackground('w') pen = pg.mkPen(color="k", width=3, s
我正在尝试在 PyQt5 的 pqytplot plotwidget 中添加光标位置的读数。我发现这段代码可以满足我的要求,但在一个独立的窗口中,所有内容都在一个程序文件中: import pyqtg
我正在尝试使 PlotWidget 移动(从左到右)非常快,就像它是心电图一样。这是我想要的示例(除了他使用 PyQwt(这与我的项目不兼容,因为我使用 PySide(而不是 PyQt),而且我也希望
我是一名优秀的程序员,十分优秀!