Created
November 23, 2019 00:58
-
-
Save seignovert/b1eacc65e3964e1d8e0ed40d1b321e2b to your computer and use it in GitHub Desktop.
[Python] Figure watermark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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