在曲线中显示数据



我是 pyqt4 上的一个事件问题 我应该如何调试代码?我找到了我们的一些伊苏斯。检查第 5 行

import sys
matplotlib.use("Qt5Agg")
from PyQt4 import QtCore
from matplotlib.backends.backend_qt5agg import Navigation as NavigationToolbar    
FigureCanvas(self,QSizePolicy.Expanding,QSizePolicy.Expanding                               
class MatplotlibWidget(QWidget):
def __init__(self, parent=None):
super(MatplotlibWidget, self).__init__(parent)
self.initUi()
def initUi(self):
self.layout = QVBox(self)
self.mpl = plut(self, width=100, height=20, dpi=100)

您有以下错误

  • 正在画布内不必要地创建画布。
  • 如果您使用画布,则不应使用 pyplot,但使用轴子图

考虑到上述情况,解决方案是:

class MyMplCanvas(FigureCanvas):
def __init__(self, parent=None, width=10, height=4, dpi=100):
FigureCanvas.__init__(self, Figure(figsize=(width, height), dpi=dpi))
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.axes = self.figure.add_subplot(111)
self.mpl_connect("motion_notify_event", self.on_move)
self.po_annotation = []
def start_static_plot(self):
for i in range(0, 10):
x = i
y = x ** 2
(point,) = self.axes.plot(x, y, "-o")
annotation = self.axes.annotate(
("x=" + str(x), "y=" + str(y)),
xy=(x + 0.7, y + 0.7),
xycoords="data",
xytext=(x + 0.1, y + 0.1),
textcoords="data",
horizontalalignment="left",
arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=+0.2"),
bbox=dict(boxstyle="round", facecolor="w", edgecolor="0.1", alpha=0.9),
)
annotation.set_visible(False)
self.po_annotation.append([point, annotation])
self.axes.set_xlim(-0.5, 10)
self.axes.set_ylim(-5, 9.5 ** 2)
def on_move(self, event):
visibility_changed = False
for point, annotation in self.po_annotation:
should_be_visible = point.contains(event)[0]
if should_be_visible != annotation.get_visible():
visibility_changed = True
annotation.set_visible(should_be_visible)
if visibility_changed:
self.draw()

最新更新