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?
If I understood your question correctly, your code does set the params as needed except for the figure title. That is because you are using a subplot which has 'suptitle'. So the parameter you set for figure.titlesize
will not work! Check this instead:
import matplotlib
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as Canvas
import matplotlib.pyplot as plt
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"] = 'large' # this will not work since it is not for a subplot
matplotlib.rcParams["axes.titlesize"] = 45
matplotlib.rcParams["axes.labelsize"] = 45
matplotlib.rcParams["axes.linewidth"] = 1.5
matplotlib.rcParams["xtick.labelsize"] = 10
matplotlib.rcParams["ytick.labelsize"] = 10
matplotlib.rcParams["font.sans-serif"] = "Verdana"
@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(filename)
def clearfig(self):
self.clf()
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())