【问题标题】:Can't get python script to output a graph?无法让 python 脚本输出图形?
【发布时间】:2021-12-28 22:33:20
【问题描述】:

我是这个平台以及整个软件开发和编码的新手。我需要一些帮助才能让我的 python 脚本工作并输出图表。我让它在 MATLAB 中工作,而不是在 Python 中,当我在 VSCode 中运行调试时没有任何错误,但它没有按我想要的那样工作。

由于某些原因,在绘图部分它没有解析 max_range(黄色波浪线),但它在代码的其他地方被解析。我不确定这是怎么坏的?

我的程序在 GitHub 上的一个开放 repo 中,所以如果可以的话,请看看并提供帮助!

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

我最终想让程序与 GUI 一起工作,但我还没有遇到这个障碍。

谢谢

python 脚本: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)

【问题讨论】:

    标签: python github visual-studio-code


    【解决方案1】:

    Max_range 未在您提供的代码中定义。你忘记了一些事情。此外,你不需要数学模块,因为你有 numpy.因此,我将涉及“数学”的语句替换为使用“numpy”的等价语句。这是您的代码的工作版本: 注意:我注释掉了不必要的导入。

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

    最好的问候, 斯蒂芬

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 1970-01-01
      • 2021-07-03
      • 2018-06-12
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2022-11-23
      相关资源
      最近更新 更多