1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] rcparams not being applied to custom matplotlib class

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 14:53.

  1. Stack

    Stack Membro Participativo

    I am trying to write a custom figure class around matplotlib.figure.Figure which among other things, automatically applies the correct formatting. Here's the current configuration:

    import matplotlib
    from matplotlib.axes import Axes
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as Canvas

    class CustomFigure(Figure):
    def __init__(self, figsize: tuple, layout: str):
    super().__init__(figsize=figsize, layout=layout)
    self.canvas = Canvas(self)
    matplotlib.use("QtAgg")
    self.set_common_params()


    def generate_axes(self, num_axes: int, layout: tuple = None) -> Axes:
    if layout is None:
    layout = (1, num_axes)
    return self.subplots(*layout)


    def set_common_params(self):
    matplotlib.rcParams["figure.titlesize"] = 45
    matplotlib.rcParams["axes.titlesize"] = 13
    matplotlib.rcParams["axes.labelsize"] = 12
    matplotlib.rcParams["axes.linewidth"] = 1.5
    matplotlib.rcParams["xtick.labelsize"] = 11
    matplotlib.rcParams["ytick.labelsize"] = 11


    @staticmethod
    def set_labels(ax: Axes, xlabel: str, ylabel: str, title: str = None):
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    if title is not None:
    ax.set_title(title)


    def generate_pdf(self, filename: str):
    self.savefig(f"{filename}.pdf")




    if __name__ == "__main__":
    import sys
    from PySide6.QtWidgets import QApplication, QMainWindow
    app = QApplication(sys.argv)
    win = QMainWindow()
    fig = CustomFigure((5,5), "tight")
    fig.set_labels(fig.generate_axes(1), "X", "Y", "Title")
    win.setCentralWidget(fig.canvas)
    win.show()
    sys.exit(app.exec())


    I've tried putting the rcParams in every possible location of the code, even before the class definition, but nothing works.

    How do I get the rcParams applied properly?

    Continue reading...

Compartilhe esta Página