The Ipython console in Spyder uses the inline
backend, which saves the figure as png and displays the output image. When saving, it uses the option bbox_inches = "tight"
.
So in order to obtain the same figure as shown in the console, you may decide to use this option as well - it basically extends or shrinks the bounding box such that all objects in the canvas are displayed.
plt.savefig('testfig.png',dpi=300, bbox_inches = "tight")
Alternatively, you can make sure that all objects are already inside the figure boundaries before saving or showing the figure. This can either be accomplished using
plt.tight_layout()
which tries to do that automatically, or you can use
plt.subplots_adjust(left=0.3, right=0.9, bottom=0.3, top=0.9)
where the parameters denote the margins on each side in units of fractions of figure size (30% space on the left, 10% space on the right, etc.).