Home > database >  Can't get python script to output a graph?
Can't get python script to output a graph?

Time:11-18

I'm a complete novice with this platform and software development and coding as a whole. I need some help getting my python script to work and output a graph. I have it working in MATLAB, not in Python, when I run debug in VSCode there isn't any errors but it isn't working as I want it so.

For some reason in the plot section it doesn't resolve max_range (yellow squiggly line), but it is resolved elsewhere in the code. I'm not sure how this is broken?

My programme is in an open repo in GitHub, so please have a look and help if you can!

https://github.com/ashfletch/projectile-motion-project/blob/main/projectile_motion/projectile.py

I eventually want to get the programme to work with a GUI, but I haven't got to that hurdle just yet.

Thanks

Ash

python script: projectile_motion.py

import argparse
import logging
import math
import os
import sys
import tkinter as tk
from tkinter import filedialog, PhotoImage

import matplotlib.pylab as plt
import numpy as np

def trajectory(x0: int, y0: int, v0: int, theta: int, g: float) -> tuple:
    launch_angle = (theta * (math.pi / 180)) # Converts launch angle into radians
    max_range = int(((v0**2) / g) * (math.sin(2 * launch_angle))) # Calculates range in x-direction (using vector multiplication)
    x_step = int(max_range / 100) # Calculates step, using 100 values for plotting up to the range
    x_values = []
    for value in range(x0, max_range, x_step):
        x_values.append(value)
    
    y_values = []
    for x in x_values:
        y_values.append(((x * math.tan(launch_angle)) - g / (2 * (v0**2) *
         (math.cos(launch_angle))**2) * x**2)   y0)
    return (x_values, y_values)


def projectilemotion(x0: int, y0: int, v0: int, theta: int, g: float):
    pass
    """projectlemotion requires 5 user input arguments which are as follows;
    Args:
        x0: Initial displacement in 'x' domain in [m].
        y0: Initial displacement in 'y' domain in [m].
        v0: Initial velocity of projectile in [m/s].
        theta: launch angle of projectile in [degrees]
        g: The acceleration due to gravity for either Earth or Moon in [m/s**2].
    Returns:
        A plotted trajectory of the projectile under the conditions defined by
          the args above, calculating the following outputs:
        
        xPeak: the value of x [m] at which the yPeak is achieved.
        yPeak: the maximum displacement in y-direction [m].
        maxRange: the maximum displacement in x-direction [m].
    
    Raises:
        Error: 
    """ 

def plot(x_values, y_values):
    plt.plot(x_values, y_values)
    plt.axis([0, max_range, 0, max(y_values)])
    plt.xlabel('Range [m]')
    plt.ylabel('Height [m]')
    plt.show()


if __name__ == '__main__':
    trajectory(0, 0, 940, 45, 9.81)

CodePudding user response:

Max_range is not defined in the code you give. And you forgot a few things. Also, you don't need the math module since you have numpy. So, I replaced the statements involving "math" with the equivalent using "numpy". Here is a working version of your code: Note: I commented out unnecessary imports.

#import argparse # For the moment, you don't use it. But you can uncomment it later.
#import logging
#import math # you don't need it since you have numpy
#import os
#import sys
#import tkinter as tk
#from tkinter import filedialog, PhotoImage

import matplotlib.pylab as plt
import numpy as np

def trajectory(x0: int, y0: int, v0: int, theta: int, g: float) -> tuple:
    launch_angle = (theta * (np.pi / 180)) # Converts launch angle into radians
    max_range = int(((v0**2) / g) * (np.sin(2 * launch_angle))) # Calculates range in x-direction (using vector multiplication)
    x_step = int(max_range / 100) # Calculates step, using 100 values for plotting up to the range
    x_values = []
    for value in range(x0, max_range, x_step):
        x_values.append(value)
    
    y_values = []
    for x in x_values:
        y_values.append(((x * np.tan(launch_angle)) - g / (2 * (v0**2) *
         (np.cos(launch_angle))**2) * x**2)   y0)
    return (x_values, y_values)


def projectilemotion(x0: int, y0: int, v0: int, theta: int, g: float):
    pass
    """projectlemotion requires 5 user input arguments which are as follows;
    Args:
        x0: Initial displacement in 'x' domain in [m].
        y0: Initial displacement in 'y' domain in [m].
        v0: Initial velocity of projectile in [m/s].
        theta: launch angle of projectile in [degrees]
        g: The acceleration due to gravity for either Earth or Moon in [m/s**2].
    Returns:
        A plotted trajectory of the projectile under the conditions defined by
          the args above, calculating the following outputs:
        
        xPeak: the value of x [m] at which the yPeak is achieved.
        yPeak: the maximum displacement in y-direction [m].
        maxRange: the maximum displacement in x-direction [m].
    
    Raises:
        Error: 
    """ 

def plot(x_values, y_values):
    plt.plot(x_values, y_values)

    #plt.axis([0, max_range, 0, max(y_values)]) 
    #Commented because max_range is nowhere defined
    
    plt.xlabel('Range [m]')
    plt.ylabel('Height [m]')
    plt.show()


if __name__ == '__main__':
    x_values, y_values= trajectory(0, 0, 940, 45, 9.81)
    # because trajectory returns 2 vectors...

    # and you forgot to call plot
    plot(x_values,y_values)

Best regards, Stéphane

  • Related