Instead of plt.show(), use these lines: import saveplots saveplots.save(__file__) For pasting Python code in vi, first do :set paste to tell vi to be in paste mode without adding extra indentations.