Python PyQt4,matplotlib,修改现有打印的轴标签
我正在PyQt4和matplotlib中创建绘图。下面这个过于简化的演示程序显示,我想更改轴上的标签以响应某些事件。为了在这里演示,我制作了一个“指针输入”事件。该程序的行为是,我只是没有得到任何变化的外观,情节Python PyQt4,matplotlib,修改现有打印的轴标签,python,matplotlib,pyqt4,Python,Matplotlib,Pyqt4,我正在PyQt4和matplotlib中创建绘图。下面这个过于简化的演示程序显示,我想更改轴上的标签以响应某些事件。为了在这里演示,我制作了一个“指针输入”事件。该程序的行为是,我只是没有得到任何变化的外观,情节 import sys from PyQt4.QtGui import * from PyQt4.QtCore import * from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanva
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_()
你就快到了。您只需指示matplotlib在完成调用函数(如
set\u xlabel()
)后重新绘制绘图即可
按如下方式修改您的程序:
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))
self.canvas.draw()
现在,每次将鼠标移动到窗口中时,您都会看到标签的更改 谢谢。通常,在什么情况下需要调用draw()来更新绘图的外观?如果有人偶然发现了这一点,我会遇到类似的问题:即使我正在调用
set\u xlabel
,我的轴标签也不会显示。原来这是一个排序问题,在绘制数据后必须调用集标签。