如何在 matplotlib 小部件的嵌入式图形上使用跨度选择器?
How to use the Span Selector on a embedded figure of matplotlib widget?
我在 GUI 上工作,我有一个带图形的系统。
我想在可视化的图形中使用 spanselector。
我已经搜索过了,但我不明白如何在调用 matplotlib 小部件时使用跨度选择器。
这是我要绘制的示例。它有 3 个部分(主要,mplwidget,ui 文件)
主代码文件
# ------------------------------------------------------
# ---------------------- main.py -----------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
import numpy as np
import random
#from matplotlib.widgets import SpanSelector
class MatplotlibWidget(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("qt_designer.ui",self)
self.setWindowTitle("PyQt5 & Matplotlib Example GUI")
self.pushButton_generate_random_signal.clicked.connect(self.update_graph)
self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
def update_graph(self):
fs = 500
f = random.randint(1, 100)
ts = 1/fs
length_of_signal = 100
t = np.linspace(0,1,length_of_signal)
cosinus_signal = np.cos(2*np.pi*f*t)
sinus_signal = np.sin(2*np.pi*f*t)
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.plot(t, cosinus_signal)
self.MplWidget.canvas.axes.plot(t, sinus_signal)
self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'),loc='upper right')
self.MplWidget.canvas.axes.set_title('Cosinus - Sinus Signal')
self.MplWidget.canvas.draw()
#span = self.MplWidget.canvas.axes.SpanSelector(ax1, onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red'))
def onselect(min_value, max_value):
print(min_value, max_value)
return min_value, max_value
app = QApplication([])
window = MatplotlibWidget()
window.show()
app.exec_()
mplwidget 文件
# ------------------------------------------------------
# -------------------- mplwidget.py --------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MplWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.canvas = FigureCanvas(Figure())
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
self.canvas.axes = self.canvas.figure.add_subplot(111)
self.setLayout(vertical_layout)
ui 文件附在这个 link 上,其中包含所有代码:
here
换句话说,这是使用跨度选择器的示例代码:
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [10, 50, 100])
def onselect(vmin, vmax):
print(vmin, vmax)
rectprops = dict(facecolor='blue', alpha=0.5)
span = mwidgets.SpanSelector(ax, onselect, 'horizontal',span_stays=True,button=1,,rectprops=rectprops)
fig.show()
////////////////////////////////////////// ///////////////////////////////
我尝试了很多方法来评估跨度选择器,但我对它的工作方式以及我应该如何连接代码结构有点困惑?
if i 运行 whithin 注释行:
span = self.MplWidget.canvas.axes.SpanSelector(ax1, onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red'))
它显示了以下错误:
属性错误:'AxesSubplot' object has no attribute 'SpanSelector'
终于有了想要的结果
您必须将 self.MplWidget.canvas.axes
作为 ax:
# ...
self.MplWidget.canvas.draw()
self.span = SpanSelector(
<b>self.MplWidget.canvas.axes</b>,
<b>self.onselect</b>,
"horizontal",
useblit=True,
rectprops=dict(alpha=0.5, facecolor="red"),
)
def onselect(<b>self</b>, min_value, max_value):
print(min_value, max_value)
注意:因为select
是class的一个方法,它必须有self
作为第一个参数,必须用self.select
.
调用
我在 GUI 上工作,我有一个带图形的系统。
我想在可视化的图形中使用 spanselector。
我已经搜索过了,但我不明白如何在调用 matplotlib 小部件时使用跨度选择器。
这是我要绘制的示例。它有 3 个部分(主要,mplwidget,ui 文件)
主代码文件
# ------------------------------------------------------
# ---------------------- main.py -----------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
import numpy as np
import random
#from matplotlib.widgets import SpanSelector
class MatplotlibWidget(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("qt_designer.ui",self)
self.setWindowTitle("PyQt5 & Matplotlib Example GUI")
self.pushButton_generate_random_signal.clicked.connect(self.update_graph)
self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
def update_graph(self):
fs = 500
f = random.randint(1, 100)
ts = 1/fs
length_of_signal = 100
t = np.linspace(0,1,length_of_signal)
cosinus_signal = np.cos(2*np.pi*f*t)
sinus_signal = np.sin(2*np.pi*f*t)
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.plot(t, cosinus_signal)
self.MplWidget.canvas.axes.plot(t, sinus_signal)
self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'),loc='upper right')
self.MplWidget.canvas.axes.set_title('Cosinus - Sinus Signal')
self.MplWidget.canvas.draw()
#span = self.MplWidget.canvas.axes.SpanSelector(ax1, onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red'))
def onselect(min_value, max_value):
print(min_value, max_value)
return min_value, max_value
app = QApplication([])
window = MatplotlibWidget()
window.show()
app.exec_()
mplwidget 文件
# ------------------------------------------------------
# -------------------- mplwidget.py --------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MplWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.canvas = FigureCanvas(Figure())
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
self.canvas.axes = self.canvas.figure.add_subplot(111)
self.setLayout(vertical_layout)
ui 文件附在这个 link 上,其中包含所有代码: here
换句话说,这是使用跨度选择器的示例代码:
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [10, 50, 100])
def onselect(vmin, vmax):
print(vmin, vmax)
rectprops = dict(facecolor='blue', alpha=0.5)
span = mwidgets.SpanSelector(ax, onselect, 'horizontal',span_stays=True,button=1,,rectprops=rectprops)
fig.show()
////////////////////////////////////////// ///////////////////////////////
我尝试了很多方法来评估跨度选择器,但我对它的工作方式以及我应该如何连接代码结构有点困惑?
if i 运行 whithin 注释行:
span = self.MplWidget.canvas.axes.SpanSelector(ax1, onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red'))
它显示了以下错误:
属性错误:'AxesSubplot' object has no attribute 'SpanSelector'
终于有了想要的结果
您必须将 self.MplWidget.canvas.axes
作为 ax:
# ...
self.MplWidget.canvas.draw()
self.span = SpanSelector(
<b>self.MplWidget.canvas.axes</b>,
<b>self.onselect</b>,
"horizontal",
useblit=True,
rectprops=dict(alpha=0.5, facecolor="red"),
)
def onselect(<b>self</b>, min_value, max_value):
print(min_value, max_value)
注意:因为select
是class的一个方法,它必须有self
作为第一个参数,必须用self.select
.