Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matplotlib savefig limited to size of the path #17313

Closed
hiyamgh opened this issue May 3, 2020 · 8 comments
Closed

matplotlib savefig limited to size of the path #17313

hiyamgh opened this issue May 3, 2020 · 8 comments
Labels
status: needs clarification Issues that need more information to resolve.

Comments

@hiyamgh
Copy link

hiyamgh commented May 3, 2020

I have been trying to save a figure using matplotlib. The size of the path I specify to plt.savefig was too long. Specifically:

# I am using the following
plt.savefig(os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name))

The output of os.path.join(output_folder, 'forecasts_scatter_%s.png' % model_name) is:

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

But I was getting the error:

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'

I replaced this with:

# made the figure name shorter: 'scatter_%s.png' instead of 'forecasts_scatter_%s.png'
plt.savefig(os.path.join(output_folder, 'scatter_%s.png' % model_name))

and it worked.

@QuLogic
Copy link
Member

QuLogic commented May 4, 2020

Does open('../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', 'wb') work in Python console?

If not, then this is a Python/Windows issue and I'm not sure there's much we can do about it.

@skypaver
Copy link

skypaver commented Jul 2, 2020

2020-07-02 (2)

That's might be the reason.
These characters cannot be included in files' name.

@QuLogic QuLogic added the status: needs clarification Issues that need more information to resolve. label Jul 2, 2020
@dstansby
Copy link
Member

I'm going to close this since it doesn't look like it is a Matplotlib issue - if you can reply to @QuLogic's comment above, or provide more info feel free to re-open this issue or open a new one!

@apohali
Copy link

apohali commented Feb 6, 2023

I have a similar problem. The length of the name of file or the parent directory needs to be short. Otherwise I get FileNotFoundError: [Errno 2] No such file or directory. I have tried saving in a different directory using the same filename and it worked fine. So the problem is not use of characters that cannot be used in filename.

@jklymak
Copy link
Member

jklymak commented Feb 6, 2023

@apohali Please open a new issue with steps needed to reproduce the problem. However please check that a normal python file-open works. If not, we can't help.

@qzdeng
Copy link

qzdeng commented Aug 16, 2023

I got the same issue. After testing, the problem seems to be that the overall character amount in fname must be < 256

@rcomer
Copy link
Member

rcomer commented Aug 16, 2023

@qzdeng did you also try a normal file-open similar to #17313 (comment)?

@qzdeng
Copy link

qzdeng commented Aug 16, 2023

@qzdeng did you also try a normal file-open similar to #17313 (comment)?

True, it is a Windows caused problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs clarification Issues that need more information to resolve.
Projects
None yet
Development

No branches or pull requests

8 participants