Skip to content

Commit

Permalink
🎨 dpi argument and tight_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
enryh committed Dec 23, 2024
1 parent b3a905e commit 15a1540
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/njab/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from njab.plotting.lifelines import plot_lifelines

__all__ = ['plot_lifelines']
Expand All @@ -26,19 +25,24 @@

def savefig(fig: matplotlib.figure.Figure,
name: str,
folder: pathlib.Path = '.',
pdf=True):
folder: str = '.',
dpi: int = 300,
pdf:bool=True,
tight_layout:bool=True) -> None:
"""Save matplotlib Figure (having method `savefig`) as pdf and png."""
folder = pathlib.Path(folder)
fname = folder / name
folder = fname.parent # in case name specifies folders
folder.mkdir(exist_ok=True, parents=True)
if not fig.get_constrained_layout():
if tight_layout and not fig.get_constrained_layout():
fig.tight_layout()
fig.savefig(fname.with_suffix('.png'), bbox_inches='tight')
fname = fname.with_suffix('.png')
fig.savefig(fname, dpi=dpi,bbox_inches='tight')
logger.info("Saved Figures to %s", fname)
if pdf:
fig.savefig(fname.with_suffix('.pdf'), bbox_inches='tight')
logger.info(f"Saved Figures to {fname}")
fname = fname.with_suffix('.pdf')
fig.savefig(fname, bbox_inches='tight')
logger.info("Saved Figures to %s", fname)


def select_xticks(ax: matplotlib.axes.Axes, max_ticks: int = 50) -> list:
Expand Down

0 comments on commit 15a1540

Please sign in to comment.