matplotlib savefig - 没有这样的文件或目录

Per*_*erl 6 python matplotlib

我的 matplotlib 函数有一个非常奇怪的行为savefig。我正在做如下:

import os

# creating the output folder 
if not os.path.exists(output_folder):
   os.makedirs(output_folder)

# adding the name of the figure to the created output folder
plt.savefig(output_folder + 'forecasts_scatter_%s' % model_name)
# plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))
plt.close()
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "C:/Users/96171/Desktop/ministry_of_public_health/CodeUbrCorrected/bmwmlmcw.py", line 56, in <module>
    lm.cross_validation(model, hyperparameters[model_name], model_name)
  File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 554, in cross_validation
    self.cross_validation_grid(model_used, hyperparams, model_name)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\testing.py", line 348, in wrapper
    return fn(*args, **kwargs)
  File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 1444, in cross_validation_grid
    'predicted')
  File "C:\Users\96171\Desktop\ministry_of_public_health\CodeUbrCorrected\cross_validation_smogn.py", line 1772, in plot_actual_vs_predicted_scatter_bisector
    plt.savefig(output_folder + 'forecasts_scatter_%s' % model_name)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 689, in savefig
    res = fig.savefig(*args, **kwargs)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\figure.py", line 2094, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backend_bases.py", line 2075, in print_figure
    **kwargs)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 521, in print_png
    cbook.open_file_cm(filename_or_obj, "wb") as fh:
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 407, in open_file_cm
    fh, opened = to_filehandle(path_or_file, mode, True, encoding)
  File "C:\Users\96171\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 392, in to_filehandle
    fh = open(fname, flag, encoding=encoding)
FileNotFoundError: [Errno 2] No such file or directory: '../output_ubr_shallow/all_columns_minus_weather_minus_lags_minus_civilians_balance/smogn_0.95_Manhattan_rel_mat1/grid_search/train_test_forecasts_scatterplot_bisector/forecasts_scatter_lasso.png'
Run Code Online (Sandbox Code Playgroud)

我指定的输出是相对路径

'../output_ubr_shallow/all_columns_minus_weather_minus_lags_minus_civilians_balance/smogn_0.95_Manhattan_rel_mat1/grid_search/train_test_forecasts_scatterplot_bisector/'
Run Code Online (Sandbox Code Playgroud)

当我这样做时,os.path.exists(output_folder)它给了我 True。

我知道我们可以在savefig. 否则我做错了什么?

我也尝试过这个:

# changing the .png thing and using os.path.join
plt.savefig(os.path.join(output_folder, 'forecasts_scatter_' + model_name + '.png'))
Run Code Online (Sandbox Code Playgroud)

和这个:

# changing the .png thing and using os.path.join
plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))
Run Code Online (Sandbox Code Playgroud)

但仍然得到相同的结果。

很奇怪的是我从不同的脚本运行相同的代码。我对代码做了一些修改,使其如下:

 p = output_folder + 'forecasts_scatter_' + model_name + '.png'
 filename = os.path.split(p)[1]
 directory = os.path.split(p)[0]

 if not os.path.exists(directory):
    os.makedirs(directory)

 savepath = os.path.join(directory, filename)
 print('save path isss: {}'.format(savepath))
 plt.savefig(savepath)
 plt.close()
Run Code Online (Sandbox Code Playgroud)

第一个脚本将此作为输出路径:

save path isss: ../output_ubr_shallow/all_columns_balance/smogn_0.95_Manhattan_rel_mat1/grid_search/train_test_forecasts_scatterplot_bisector\forecasts_scatter_lasso.png
Run Code Online (Sandbox Code Playgroud)

第二个脚本将此作为输出路径

save path isss: ../output_ubr_shallow/all_columns_minus_weather_minus_lags_minus_civilians_balance/smogn_0.95_Manhattan_rel_mat1/grid_search/train_test_forecasts_scatterplot_bisector\forecasts_scatter_lasso.png
Run Code Online (Sandbox Code Playgroud)

第一个有效,第二个无效。这两个脚本都在同一目录中找到!

Per*_*erl 13

哇,在花了很多时间之后,我刚刚替换了这个:

这:

plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))
Run Code Online (Sandbox Code Playgroud)

这样

plt.savefig(os.path.join(output_folder, 'scatter_%s.png' % model_name))
Run Code Online (Sandbox Code Playgroud)

它起作用了。我不确定这是否是 matplotlib 中可能存在的错误。我认为路径的长度savefig有一些限制。

我在github上打开了一个问题https://github.com/matplotlib/matplotlib/issues/17313