我正在使用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_()
set_xlabel
。事实证明这是一个顺序问题,必须在绘制数据后调用set_xlabel
。 - Rodrigo