【问题标题】:Save Spyder console output containing figures保存包含数字的 Spyder 控制台输出
【发布时间】:2018-09-17 21:11:15
【问题描述】:

我在 Spyder 控制台中有很多绘图结果输出。有什么简单的方法可以将这些结果保存在一个大文件中,比如 pdf 等。目前我必须手动复制每个图。

谢谢。

【问题讨论】:

标签: python pdf plot save spyder


【解决方案1】:

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

【讨论】:

  • 是否有任何 python 函数可以以编程方式执行此操作?
  • @UtkarshPanwar 你做到了吗?我需要和 python 函数一样。
  • @innuendo,抱歉,无法以编程方式执行此操作。
【解决方案2】:

如果您使用的是 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

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

【讨论】:

    【解决方案3】:

    所以我似乎遇到了这个问题,并找到了一个漂亮的 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')
    

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 2019-02-22
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2019-11-10
      • 2019-05-01
      相关资源
      最近更新 更多