Skip to content

Instantly share code, notes, and snippets.

@mrnabati
Created September 22, 2019 14:12
Show Gist options
  • Select an option

  • Save mrnabati/f0a46c62ddac0bb57d8f38831ae194f8 to your computer and use it in GitHub Desktop.

Select an option

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
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