Python 在GUI嵌入式绘图中使用matplotlib光标小部件
我有一个PyQt4 GUI,其中嵌入了matplotlib绘图。我想在绘图中添加一个Python 在GUI嵌入式绘图中使用matplotlib光标小部件,python,matplotlib,plot,pyqt,Python,Matplotlib,Plot,Pyqt,我有一个PyQt4 GUI,其中嵌入了matplotlib绘图。我想在绘图中添加一个光标小部件(,它对我有用)。由于某种原因,光标没有显示在嵌入的绘图中。发生什么事了 下面是一个最小(非)工作示例 import sys from PyQt4 import QtGui from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_q
光标
小部件(,它对我有用)。由于某种原因,光标没有显示在嵌入的绘图中。发生什么事了
下面是一个最小(非)工作示例
import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
import random
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
plt.style.use('ggplot')
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
#self.toolbar.hide()
# Just some button
self.button = QtGui.QPushButton('Plot')
self.button.clicked.connect(self.plot)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
def plot(self):
''' plot some random stuff '''
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
ax.hold(False)
ax.plot(data, '*-')
Cursor(ax, lw = 2)
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.setWindowTitle('Simple QTpy and MatplotLib example with Zoom/Pan')
main.show()
sys.exit(app.exec_())
从:
要使光标保持响应,必须保留对它的引用
最简单的方法是将其分配给类变量,self.cursor=cursor(…)
def绘图(自):
“画一些随机的东西”
数据=[random.random()表示范围内的i(25)]
ax=自身图添加子图(111)
#ax.hold(错误)
def plot(self):
''' plot some random stuff '''
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
#ax.hold(False) <- don't use ax.hold!
ax.plot(data, '*-')
self.cursor = Cursor(ax, lw = 2)
self.canvas.draw()