I'm trying to save the Python graph as a picture, but it's

Asked 2 years ago, Updated 2 years ago, 115 views

// Enter code here

'''
Relationship between distance and tensile force between two objects
'''
from pylab import figure, axes, pie, title, savefig
import matplotlib.pyplot as plt

#Draw Graphs
def draw_graph(x, y):
    plt.plot(x, y, marker='o')
    plt.xlabel('Distance in meters')
    plt.ylabel('Gravitational force in newtons')
    plt.title('Gravitational force and distance')
    plt.show()

def generate_F_r():
    To generate a value for #r
    r=range(100, 1001, 50)
    A blank list to store the value of #F
    F=[]

    #constant, G
    G=6.674*(10**-11)
    #Two mass values
    m1 = 0.5
    m2 = 1.5

    #Calculate the force and add it to list F.
    for dist in r:
        force = G*(m1*m2)/(dist**2)
        F.append(force)

    Invoke the #draw_graph function.
    draw_graph(r, F)

if __name__=='__main__':
    generate_F_r()

savefig('GG.pdf')
savefig('C:\GG.pdf')


I'd appreciate it if you could help me <

python graph image save

2022-09-22 20:18

1 Answers

You must assign the current image instance to the variable with gcf() and then call savefig() with this instance. Please change the drawing_graph() as follows

def draw_graph(x, y):
    plt.plot(x, y, marker='o')
    plt.xlabel('Distance in meters')
    plt.ylabel('Gravitational force in newtons')
    plt.title('Gravitational force and distance')

    Fig = plt.gcf() #Changed
    plt.show()
    fig.savefig('GG.pdf') #Where did you change it?


2022-09-22 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.