I am trying to save a Matplotlib figure as a file from an iPython notebook.
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_axes([1,1,1,1]) ax.plot([1,2]) fig.savefig('test.png')
The inline view in the iPython notebook looks good:
The file 'test.png' is almost empty though. It looks like the plot is shifted to the top right, you can see the tick labels '1.0' and '0.0' in the corner.
How can I produce a file from the iPython notebook that looks like the inline view?
Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.
Problem solved: add 'bbox_inches='tight'
argument to savefig.
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_axes([1,1,1,1]) plt.plot([1,2]) savefig('test.png', bbox_inches='tight')
I don't understand what's happening here, but the file looks like the iPython notebook inline file now. Yay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With