3

I am trying to learn Python. I am using the following program, which creates the image OK, but I want to save it. I tried some instructions I found on this site, but am getting the error message at the bottom. Any help would be appreciated.

Program:

import sys
import random
import matplotlib as plt
from graphics import *

def main():
    m=1
    n=2
    offset=50
    win = GraphWin("MyWin",500, 500)
    win.setBackground(color_rgb(0,0,0))
    for i in range(1,1000,1):
        r= random.uniform(0,1)
        q= int(3*r)
        m = (m/2) + q*(q-1)*75
        n = (n/2) + q*(3-q)*75

        pt = Point(m + offset,n + offset)
        pt.setOutline(color_rgb(255,255,0))
        pt.draw(win)

    print("graphic done")
    plt.savefig("figure.png")
    win.getMouse()
    win.close()

if __name__ == '__main__':
    main()

Error Message:

graphic done
Traceback (most recent call last):
File "fractal_1.py", line 29, in <module> main()
File "fractal_1.py", line 24, in main
plt.savefig("figure.png")
AttributeError: module 'matplotlib' has no attribute 'savefig'

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
paladin7429
  • 39
  • 2
  • 3
  • What is your `plt`? It must be a reference to `matplotlib.pyplot` for your code to work. – DYZ Jan 27 '18 at 00:53

2 Answers2

1

The call to plt.savefig("figure.png") will only work if you have imported as follows: import matplotlib.pyplot as plt.

I believe your error lies with plt and what it actually references. If you imported like this:import matplotlib as plt then you would need to call the required function like this: plt.pyplot.savefig("figure.png")

If you imported like this:import matplotlib.pyplot as plt then you can call the required function like this: plt.savefig("figure.png")

Jexodus
  • 30
  • 3
  • These suggestions did not work. I am using Python 3.6.3. I installed matplotlib, and it seemed to install correctly (there were no noted errors). Is pyplot be included in matplotlib or is that a separate installation? Is matplotlib even what I need since I am not actually making a plot, in the tradional sense? I am just lighting pixels (a fractal) and want to save the image. Sorry for the low level of experience, but thanks to all for the help. – paladin7429 Jan 27 '18 at 21:33
  • Easy way to check if it is installed correctly is to open terminal and open the python program. Then `import matplotlib.pyplot as plt` and run the help command on the savefig() function - `help(plt.savefig)`. Note there is no parenthesis at the end of savefig when using the help function. If the help is displayed, it is installed properly. This function will also help you answer your question as to if it is the right function to use (I haven't used matplotlib myself) – Jexodus Jan 27 '18 at 22:08
0

Thanks for including the error, but what that's saying is that matplotlib doesn't have a savefig() function.

I think it's matplotlib.pyplot.savefig() as per this link: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.savefig.html

Edit your code to say: plt.pyplot.savefig().

ACVM
  • 1,497
  • 8
  • 14