python - PyQt4,matplotlib,修改现有绘图的轴标签

标签 python matplotlib pyqt4

我正在 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/

相关文章:

python时间序列将各个工作日绘制为线

qt - 拖放按钮和下拉菜单 PyQt/Qt designer

python-2.7 - 无法删除 imshow() 图周围的 matplotlib 填充

python - 使用不同的轴类型绘制多个图

Python 卡住的可执行文件在某些​​计算机上无法运行

python - 使用 Python OpenCV 检测图像中的对象位置

python - 从 Elasticsearch 导出的CSV文件中的可读列名称?

Python for Naoqi(动态模块未正确初始化)

python - opencv彩色图像到greycode转换错误

python - 访问散点图的 matplotlib 对象