-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
- lcdblib version: NA
- Python version: NA
- Operating System: NA
Description
I have been thinking about streamlining my plotting workflow. I am proposing a decorator to plot functions allow outputting different contexts and formats.
What I Did
My current decorator:
from functools import wraps
def make_figs(fname=None, styles=None, formats=None):
def _plot_all(func, styles=styles, formats=formats):
@wraps(func)
def wrapper(*args, styles=styles, formats=formats, **kwargs):
def plot_style(style, formats):
if isinstance(formats, str):
formats = [formats]
elif formats is None:
formats = ['png', 'eps']
with plt.style.context('seaborn-' + style):
fn = fname + '_' + style
func(*args, **kwargs)
plt.tight_layout()
if style != 'notebook':
for f in formats:
plt.savefig('{}.{}'.format(fn, f))
plt.close()
if isinstance(styles, str):
styles = [styles]
elif styles is None:
styles = ['notebook', 'talk', 'poster', 'paper']
for style in styles:
plot_style(style, formats)
return wrapper
return _plot_allTo use you can call it like so:
@make_figs(fname='test_plot', styles=['notebook', 'poster', 'paper'], formats=['png', 'svg'])
def plot(df):
fig, ax = plt.subplots(1, 1)
df.plot('X', 'Y', kind='scatter', ax=ax)
ax.set_title('test')
return figThis would then output:
test_plot_poster.pngtest_plot_poster.svgtest_plot_paper.pngtest_plot_paper.svg
The notebook style is displayed.
@daler you probably already have something that does this kind of thing. Just seeing if it is generally useful and should be be added to lcdblib. Or if I should just keep it in my personal library.
Metadata
Metadata
Assignees
Labels
No labels