我正在 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()
等函数后指示 matplotlib 重新绘制绘图。
按如下方式修改您的程序:
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()
每次将鼠标移入窗口时,您都会看到标签发生变化!
关于python - PyQt4,matplotlib,修改现有绘图的轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394382/