Draw a graph of gravity according to altitude with matplotlib

Asked 2 years ago, Updated 2 years ago, 113 views

N = 80
z = np.zeros((N,2))

for h in range(0,80,1):
    z[h,0] = h

    #constants
    mass_earth = 5.9722*10**(24) #[kg]
    radius_earth = 6371*10**(3) #[m]
    universal_gravitational_const = 6.674*10**(-11) #[m^3/kg^-1*s^2]
    standard_gravity = 9.80665 #[m/s^2]

    #calculation
    local_gravity = (universal_gravitational_const*mass_earth)/((radius_earth+h))**(2)
    z[h,1] = local_gravity


plt.title('Gravitational Acceleration at Different Altitudes',fontsize=12)
plt.ylabel('Altitude in [km]',fontsize=10)
plt.xlabel('Gravitational Acceleration [m/s²]',fontsize=10)
plt.axis([9.5,9.85,0,80])
plt.plot(z[h,1],z[h,0],'r*')
plt.show()

Hello, I think it's a really simple problem, but I'm asking because I can't figure it out. The x-axis on the graph shows the calculated gravity and the y-axis shows the altitude one by one, but when you try to plot it on the graph, it's taken like the picture below.

Help me.

python for list matplotlib

2022-09-20 18:57

2 Answers

Try without plt.axis.


2022-09-20 18:57

Modify plt.plot(z[h,1],z[h,0]'r*') to plt.plot(z[:,1],z[:,0]'r*').

import numpy as np
import matplotlib.pyplot as plt

N = 80
z = np.zeros((N,2))

for h in range(0,80,1):
    z[h,0] = h

    #constants
    mass_earth = 5.9722*10**(24) #[kg]
    radius_earth = 6371*10**(3) #[m]
    universal_gravitational_const = 6.674*10**(-11) #[m^3/kg^-1*s^2]
    standard_gravity = 9.80665 #[m/s^2]

    #calculation
    local_gravity = (universal_gravitational_const*mass_earth)/((radius_earth+h))**(2)
    z[h,1] = local_gravity


plt.title('Gravitational Acceleration at Different Altitudes',fontsize=12)
plt.ylabel('Altitude in [km]',fontsize=10)
plt.xlabel('Gravitational Acceleration [m/s²]',fontsize=10)
plt.axis([9.5,9.85,0,80])
plt.plot(z[:,1],z[:,0],'r*')
plt.show()


2022-09-20 18:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.