我正在 PyQt4 和 matplotlib 中创建绘图。以下过于简化的演示程序显示我想更改轴上的标签以响应某些事件。为了在这里演示,我做了一个“指针输入”事件。该程序的行为是我根本没有在情节的外观上得到任何改变。
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import random
class Window(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(400,400)
# set up a plot but don't label the axes
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
h = QHBoxLayout(self)
h.addWidget(self.canvas)
def enterEvent(self, evt):
# defer labeling the axes until an 'enterEvent'. then set
# the x label
r = int(10 * random.random())
self.axes.set_xlabel(str(r))
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()