Coverage for pyxfem/postpro/plot_tools.py: 0%

35 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-24 07:17 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4Created on Thu April 4 12:42:30 2024 

5 

6@author: nchevaug """ 

7from __future__ import annotations 

8import pathlib 

9import warnings 

10import matplotlib as mpl 

11import matplotlib.pyplot as plt 

12import matplotlib.figure 

13try : 

14 import tikzplotlib as tikz # pyright: ignore [reportMissingImports] 

15 HAVE_TIKZ = True 

16except ImportError: 

17 warnings.warn("tikzplot not Found tikz plot will be print has png") 

18 HAVE_TIKZ = False 

19 

20mpl.rcParams['lines.linewidth'] = 2 

21Fig = matplotlib.figure.Figure 

22 

23def figax(xlabel: str, ylabel: str): 

24 ''' simple function to create fig with names axis ''' 

25 fig, ax = plt.subplots(1, 1) 

26 ax.set_xlabel(xlabel) 

27 ax.set_ylabel(ylabel) 

28 return fig, ax 

29 

30def tikzplotlib_fix_ncols(obj): 

31 """ 

32 workaround for matplotlib 3.6 renamed legend's _ncol to _ncols, which breaks tikzplotlib 

33 """ 

34 if hasattr(obj, "_ncols"): 

35 obj._ncol = obj._ncols # pylint: disable=protected-access 

36 for child in obj.get_children(): 

37 tikzplotlib_fix_ncols(child) 

38 

39def savefig(fig : Fig, filepath: pathlib.Path|str) -> None: 

40 ''' Export the figure fig to file named filepath. if extension is .tikz or .tex, use tikzplotlib 

41 For the export 

42 ''' 

43 if isinstance(filepath, str): 

44 print("saved figs ###########" ) 

45 print(filepath) 

46 filepath = pathlib.Path(filepath) 

47 if filepath.suffix in ['.tikz', '.tex'] : 

48 tikzplotlib_fix_ncols(fig) 

49 tikz.save(filepath, figure=fig, 

50 axis_width = '\\axiswidth', 

51 axis_height = '\\axisheight') 

52 else: 

53 fig.savefig(str(filepath)) 

54 

55def show()->None: 

56 """ display interactive matplotlib figure """ 

57 plt.show()