保存包含数字的Spyder控制台输出

时间:2018-04-08 09:40:27

标签: python pdf plot save spyder

我在Spyder控制台中输出了很多情节结果。有没有简单的方法将这些结果保存在pdf等大文件中。目前我必须手动复制每个图。

感谢。

3 个答案:

答案 0 :(得分:4)

Spyder维护者在这里)您可以将所有控制台输出保存到Html文件,方法是右键单击它并选择名为Save as HTML/XML的选项,如下所示:

context-menu

答案 1 :(得分:0)

如果您使用的是matplotlib,这是解决方案:

#importing libraries
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

#just making a plot for an example
y = [2,4,6,8,10,12,14,16,18,20]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()

#The saving part of the solution
fig.savefig('plot.png')

要更改格式,只需更改扩展名,如下所示:

fig.savefig('plot.pdf')

fig.savefig("plot.pdf", format='pdf', dpi=1000, bbox_inches='tight')

来源:Matplotlib save figure to image file

请提供您的代码以获取更多帮助。

答案 2 :(得分:0)

所以看来我遇到了这个问题,发现了一个漂亮的matplotlib后端方法,该方法允许将多个图形另存为pdf文件中的不同页面。您可以使用图形对象或仅使用其编号/名称。这是我编写的代码。

#==============================================================================
# Imports
#==============================================================================
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

#==============================================================================
# Functions
#==============================================================================

def pdf_save(fig_list, file_name, path_name='/path_name/'):

  pp=PdfPages(path_name+file_name)
  for fig in fig_list:
    pp.savefig(figure=fig)

  pp.close()

  print 'PDF saved to %s' % path_name+file_name
  return


#==============================================================================
# Run as main script (testing purposes) 
#==============================================================================

if (__name__ == "__main__" ) and 0:
  plt.close('all')

  y=np.array([0,1])

  f1=plt.figure()
  plt.plot(y)
  f2=plt.figure()
  plt.plot(-y)
  plt.figure()
  plt.plot(2*y)

  pdf_save([f1,f2,3], 'test_pdf_save.pdf')