Skip to content

Instantly share code, notes, and snippets.

@seignovert
Created November 23, 2019 00:58
Show Gist options
  • Save seignovert/b1eacc65e3964e1d8e0ed40d1b321e2b to your computer and use it in GitHub Desktop.
Save seignovert/b1eacc65e3964e1d8e0ed40d1b321e2b to your computer and use it in GitHub Desktop.
[Python] Figure watermark
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
"""Watermark figure module."""
import os
from datetime import datetime as dt
AUTHOR = 'Benoit Seignovert'
def savefig(fig, fname, root='.', author=AUTHOR, fontsize=12,
transparent=True, bbox_inches='tight',
**kwargs):
"""Save matplotlib figure with author watermark.
Parameters
----------
fig: matplotlib.figure.Figure
Matplotlib figure.
fname: str
Filename of the figure.
root: str, optional
File root output directory.
author: str, optional
Figure author name.
fontsize: int, optional
Watermark fontsize.
transparent: bool, optional
Enable transparency for PNG export.
bbox_inches:
Set tight export.
See Also
--------
:py:func:`matplotlib.pyplot.savefig`
"""
ax = fig.add_axes([.9, 0, .1, 1], frameon=False)
ax.set_zorder(-1)
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.text(0, 0, f'{author} ({dt.now().date()}) - {fname}',
fontsize=fontsize, fontstyle='italic', weight='bold',
va='center', ha='center', alpha=.75, rotation=90)
fig.savefig(os.path.join(root, fname), transparent=transparent,
bbox_inches=bbox_inches, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment