2

All parts of Python on my computer were recently installed from the Enthought academic package, but use Pyscripter for editing and running code. I'm very early in my learning curve, and so could very well be overlooking some obvious things here.

When I try to create a plot and save it like so:

import matplotlib.pylab as pl
pl.hist(myEst, bins=20, range=(.1,.60))
pl.ylabel("Freq")
pl.xlabel("Success Probability")
pl.title('Histogram of Binomial Estimator')
pl.axis([0, 1, 0, 500])
pl.vlines (.34,0,500) 
pl.savefig('TestHist.png')
pl.show()

I get these errors:

Traceback (most recent call last):
  File "<editor selection>", line 9, in <module>
  File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1172, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 100, in print_figure
    FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 2017, in print_figure
    **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 450, in print_png
    filename_or_obj = file(filename_or_obj, 'wb')
IOError: [Errno 13] Permission denied: 'TestHist.png'

If I take out the pl.savefig('TestHist') line everything works fine, and I can see the plot I want, but when that line is in there I get the errors.

I've checked my backend version using pl.get_backend(), it returns 'WXAgg', which according to documentation should be able to use .png format.

I've also tried including an explicit format='png' and format=png within the savefig command, but still get errors.

Can anyone give me advice on how to proceed, or another approach for saving a plot?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user1301991
  • 21
  • 1
  • 1
  • 2

3 Answers3

3

There's nothing wrong with your code. I just tested it locally on my machine. The issue is this error:

IOError: [Errno 13] Permission denied: 'TestHist.png'

You are most likely trying to save the file somewhere that the Python process doesn't have permission to access. What OS are you on? Where are you trying to save the file?

DaveTM
  • 726
  • 5
  • 3
  • Thank you. Now that I set the directory explicitly everything works fine.I've been in a lazy mode of using Rstudio and setting it so that the working directory was the same as the script location. I was implicitly assuming that the same was going to be true here. – user1301991 Mar 30 '12 at 18:08
2

If it helps others, I made the silly mistake of not actually designating a file name and as a result had returned the same error message that lead me to this question for review.

Here is the code that was generating the error:

plt.savefig('C:\\Users\\bwarn\\Canopy', format='png')

Here is my correction that resolved (you'll see I designated the actual file and name)

plt.savefig('C:\\Users\\bwarn\\Canopy\\myplot.png', format='png')
BrandonW87
  • 21
  • 2
0

The following worked for me when I was running a neural network on my windows machine:

 image_path = 'A:/DeepLearning/Padhai/MLFlow/images/%s.png' % (expt_id)
        plt.savefig(image_path)

Or otherwise refer:

Using 'r' in front of the path