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

olhwl3o2  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(121)

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()

字符串
错误:

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'


我指定的输出是一个相对路径

'../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/'


当我做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'))


还有这个

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


但仍然得到相同的结果。
奇怪的是,我从不同的脚本运行相同的代码。我修改了一点代码,使其如下所示:

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()


第一个脚本的输出路径如下:

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


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

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


第一个是有效的,第二个不是。这两个脚本都在同一个目录中!!!

a7qyws3x

a7qyws3x1#

哇,花了很多时间在这上面,我刚刚更换了这个:
这个:

plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))

字符串
以此

plt.savefig(os.path.join(output_folder, 'scatter_%s.png' % model_name))


而且成功了我不确定这是否是matplotlib中的一个可能的bug。我认为savefig中的路径长度有一些限制。
我在github https://github.com/matplotlib/matplotlib/issues/17313上开了一个问题

smdncfj3

smdncfj32#

Pathname too long to open?中解决了此问题
它与matplotlib或Python无关,它是MS-DOS,其最大长度为260个字符。上面链接中建议的一个解决方法是使用扩展路径字符串,通过在路径前缀\\??\。我没有测试过,但在你的情况下,你应该能够做到:

output_folder = r'\\??\..\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\'
plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))

字符串

相关问题