【问题标题】:Python Saving Plots Using For LoopsPython 使用 For 循环保存绘图
【发布时间】:2017-04-18 14:44:06
【问题描述】:

我正在尝试将 Python 中的多个绘图保存到 Python 中的特定文件夹,但在尝试保存时不断出错。代码的相关部分是:

etf_name={"AGG", "EFA", "GLD", "IVV", "QQQ", "SPY", "VEA", "VOO", "VTI", "VWO"}
for q in etf_name:
    (Plot created here)
    plt.savefig("/Users/User_Name/Documents/College/'Graphs'/q+"_Golden_Cross".png")

例如对于 AGG,我试图将文件保存为 AGG_Golden_Cross.png

谢谢!

【问题讨论】:

  • plt.savefig("/Users/User_Name/Documents/College/Graphs/"+q+"_Golden_Cross.png") 类似路径+名称+extn
  • 你遇到了什么错误?
  • 我不断收到错误消息:IOError: [Errno 2] No such file or directory: "/Users/User_name/Documents/College/'Senior Year'/'Big Data in Python/'Moving Average Strategies on ETFs'/'Graphs'/AGG_Golden_Cross.png"

标签: python for-loop save directory


【解决方案1】:

显然,路径中的某个地方有错误。尝试保存在当前目录和/或验证路径。

etf_name={"AGG", "EFA", "GLD", "IVV", "QQQ", "SPY", "VEA", "VOO", "VTI", "VWO"}
for q in etf_name:
    # (Plot created here)
    plt.savefig(q + "_Golden_Cross.png")

【讨论】:

    【解决方案2】:

    你的引号有点奇怪。

     plt.savefig("/Users/User_Name/Documents/College/'Graphs'/" + q + "_Golden_Cross.png")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多