Created
September 22, 2019 14:12
-
-
Save mrnabati/f0a46c62ddac0bb57d8f38831ae194f8 to your computer and use it in GitHub Desktop.
Save the current figure in matplotlib in pdf format, removing all whitespaces around the image
This file contains hidden or 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
| def save(filepath, fig=None): | |
| ''' | |
| Save the current figure in matplotlib with no whitespace in pdf format | |
| ''' | |
| import matplotlib.pyplot as plt | |
| if not fig: | |
| fig = plt.gcf() | |
| plt.subplots_adjust(0,0,1,1,0,0) | |
| for ax in fig.axes: | |
| ax.axis('off') | |
| ax.margins(0,0) | |
| ax.xaxis.set_major_locator(plt.NullLocator()) | |
| ax.yaxis.set_major_locator(plt.NullLocator()) | |
| fig.savefig(filepath, pad_inches = 0, bbox_inches='tight', format='pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment