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

Time:05-10

I have embedded a matplotlib figure inside a plot canvas. The idea is to be able to change the figure and axes properties. But, I couldn't modify the axis after initializing. Below is a simple example:

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_()

CodePudding user response:

Apparently the 'font size' parameter in set_tick_params no, i removed it. enter image description here

  • Related