将图形放入 canvas 后如何修改图形属性

How to modify a figure properties, after placing it in a canvas

我在情节 canvas 中嵌入了一个 matplotlib 图形。这个想法是能够更改 figureaxes 属性。但是,初始化后我无法修改轴。下面是一个简单的例子:

import sys
import matplotlib
matplotlib.use('Qt5Agg')

from PyQt5 import QtCore, QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure


class MplCanvas(FigureCanvasQTAgg):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        super(MplCanvas, self).__init__(self.fig)

class MainWindow(QtWidgets.QWidget):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.layout = QtWidgets.QVBoxLayout(self)

        self.sc = MplCanvas(self, width=5, height=4, dpi=100)
        self.sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        self.layout.addWidget(self.sc)

        button = QtWidgets.QPushButton('change figure properties')
        button.clicked.connect(self.changeProp)
        self.layout.addWidget(button)
        
        self.show()

    def changeProp(self):
        # here is to change prop, e.g., tickslabel, size, color, colorbar, legend, position, ...etc
        "I have tried the following to change the ticks, but doesn't work"
        self.sc.fig.axes[0].set_xticklabels(self.sc.fig.axes[0].get_xticklabels(), fontsize=10, color='blue', rotation=90)
        "I have also tried this, but not working"
        self.sc.fig.axes[0].xaxis.set_tick_params(fontsize=10, color='blue', rotation=90)
        """
        UserWarning: FixedFormatter should only be used together with FixedLocator
        self.sc.axes.set_xticklabels(self.sc.axes.get_xticklabels(), fontsize=10, color='blue', rotation=90)
        """
        
        "What is the best API to solve this "


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    app.exec_()

显然 set_tick_params 中的 'font size' 参数不,我删除了它。 [tick_params][1]

更新后也做了重绘。但是,我未能删除警告 ''User Warning: FixedFormatter should only be used together with Fixed Locator'.

def changeProp(self):
    self.sc.fig.axes[0].set_xticklabels(self.sc.fig.axes[0].get_xticklabels(), fontsize=10, color='red',
                                        rotation=90)
    self.sc.fig.axes[0].xaxis.set_tick_params(color='green', width= 5, rotation=45)
    self.sc.draw()