paladin7429
paladin7429

Reputation: 39

What is wrong with my attempt to save my image?

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'

Upvotes: 3

Views: 5929

Answers (2)

Jexodus
Jexodus

Reputation: 42

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

Upvotes: 2

ACVM
ACVM

Reputation: 1527

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

Upvotes: 0

Related Questions