Source code for pyxfem.postpro.plot_tools

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu April 4 12:42:30 2024

@author: nchevaug """
from __future__ import annotations
import pathlib
import warnings
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.figure
try :
    import tikzplotlib as tikz # pyright: ignore [reportMissingImports]
    HAVE_TIKZ = True
except ImportError:
    warnings.warn("tikzplot not Found tikz plot will be print has png")
    HAVE_TIKZ = False

mpl.rcParams['lines.linewidth'] = 2
Fig = matplotlib.figure.Figure

[docs] def figax(xlabel: str, ylabel: str): ''' simple function to create fig with names axis ''' fig, ax = plt.subplots(1, 1) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) return fig, ax
[docs] def tikzplotlib_fix_ncols(obj): """ workaround for matplotlib 3.6 renamed legend's _ncol to _ncols, which breaks tikzplotlib """ if hasattr(obj, "_ncols"): obj._ncol = obj._ncols # pylint: disable=protected-access for child in obj.get_children(): tikzplotlib_fix_ncols(child)
[docs] def savefig(fig : Fig, filepath: pathlib.Path|str) -> None: ''' Export the figure fig to file named filepath. if extension is .tikz or .tex, use tikzplotlib For the export ''' if isinstance(filepath, str): print("saved figs ###########" ) print(filepath) filepath = pathlib.Path(filepath) if filepath.suffix in ['.tikz', '.tex'] : tikzplotlib_fix_ncols(fig) tikz.save(filepath, figure=fig, axis_width = '\\axiswidth', axis_height = '\\axisheight') else: fig.savefig(str(filepath))
[docs] def show()->None: """ display interactive matplotlib figure """ plt.show()