import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
from typing import List, Tuple, Callable
class FormatterPlotter:
def __init__(self, num_plots: int = 8, figsize: Tuple[int, int] = (8, 8)):
self.fig, self.axs = plt.subplots(num_plots, 1, figsize=figsize)
self.x = np.arange(0, 5, 0.1)
def plot_formatters(self):
formatters = [
("Форматирование строк", ticker.StrMethodFormatter("{x:.1f} км")),
("Форматирование функций", ticker.FuncFormatter(lambda x, _: f'{int(x)}')),
("NullFormatter()", ticker.NullFormatter()),
("StrMethodFormatter('{x:.3f}')", ticker.StrMethodFormatter("{x:.3f}")),
("FormatStrFormatter('%d')", ticker.FormatStrFormatter('%d')),
("FixedFormatter", (ticker.FixedLocator(np.arange(6)),
ticker.FixedFormatter(['A', 'B', 'C', 'D', 'E', 'F']))),
("ScalarFormatter()", ticker.ScalarFormatter()),
("PercentFormatter(xmax=5)", ticker.PercentFormatter(xmax=5))
]
for ax, (title, formatter) in zip(self.axs, formatters):
ax.plot(self.x, self.x if title != "Форматирование функций" else self.x - 5)
ax.set_title(title)
if isinstance(formatter, tuple):
ax.xaxis.set_major_locator(formatter[0])
ax.xaxis.set_major_formatter(formatter[1])
else:
ax.xaxis.set_major_formatter(formatter)
def show_plot(self):
plt.tight_layout()
plt.show()
def main():
plotter = FormatterPlotter()
plotter.plot_formatters()
plotter.show_plot()
if __name__ == "__main__":
main()