简体   繁体   English

使用 pylab 或 matplotlib 指定 savefig 的路径

[英]Specify path of savefig with pylab or matplotlib

I am struggling on how to find the correct way to specify the save path (or repository) when calling the function savefig in matplotlib or pylab.在 matplotlib 或 pylab 中调用函数 savefig 时,我正在努力寻找指定保存路径(或存储库)的正确方法。

I tried several syntaxes, but everytime python console returns :我尝试了几种语法,但每次 python 控制台返回:

FileNotFoundError: [Errno 2] No such file or directory: '../MyDocs/resource/frames/MyImage.png' FileNotFoundError: [Errno 2] 没有这样的文件或目录:'../MyDocs/resource/frames/MyImage.png'

Currently, I have written the following :目前,我写了以下内容:

pylab.savefig('../MyDocs/resource/frames/MyImage.png')

Does someone know how to do it?有人知道怎么做吗?

The tilde operator and the variable $HOME are entered as strings and therefore do not function while saving.波浪号运算符和变量$HOME作为字符串输入,因此在保存时不起作用。 You have to provide either relative path (as you did) or provide the full path.您必须提供相对路径(如您所做的那样)或提供完整路径。 Eg.例如。 pylab.savefig("/home/username/Desktop/myfig.png") . pylab.savefig("/home/username/Desktop/myfig.png")

For matplotlib.pyplot, the error FileNotFoundError: [Errno 2] No such file or directory can occur due to nonexistence of the containing folder ../MyDocs/resource/frames/ .对于 matplotlib.pyplot,错误FileNotFoundError: [Errno 2] No such file or directory由于包含文件夹../MyDocs/resource/frames/不存在,不会出现FileNotFoundError: [Errno 2] No such file or directory So maybe first create the folder所以也许首先创建文件夹

import os
os.makedirs('../MyDocs/resource/frames/')

then rerun the savefig function.然后重新运行 savefig 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM