优化和改进可模拟投掷棒球3D轨迹的Python代码

时间:2018-12-28 00:06:35

标签: python python-3.x numpy matplotlib data-modeling

我一直在学习Python,因为我需要扩展对编程语言的了解。阅读并观看了一些教程之后,我想到了一个基本程序,该程序使用numpy和maplotlib对从投手丘投掷到垒板的棒球进行建模。当我刚开始学习python时,我不认为我的代码是如此出色。

我要问的第一件事是提出改进建议。在开始添加之前,我想对其进行优化。提供的任何链接或仅是一般的帮助都很好。

第二,使用matplotlib的最佳方法是还是还有其他建议?

第三,我想在程序中添加一个仅是一个简单的半球形的投球丘,有人可以向我指出正确的方向吗?

感谢所有帮助/指导/意见!

我将在代码中包含我要改进的功能。

from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(111, projection = '3d')

#stike zone
def strike_zone():
    x_zone =        [0,0]
    z_sides =       [1.5, 3.5]
    y_right =       [-1, -1]
    y_right_mid =   [-.333,-.333]
    y_left =        [1,1]
    y_left_mid =    [.333,.333]
    y_sides =       [-1,1]
    z_top =         [3.5,3.5]
    z_mid_top =     [2.833,2.833]
    z_mid_bottom =  [2.167,2.167]
    z_bottom =      [1.5,1.5]
    ax1.plot(x_zone,y_left, z_sides, color = 'r',linewidth = 2) #left side
    ax1.plot(x_zone, y_right, z_sides, color = 'r',linewidth = 2) #right side
    ax1.plot(x_zone, y_right_mid, z_sides, color = 'r',linewidth = 1) #right mid side
    ax1.plot(x_zone, y_left_mid, z_sides, color = 'r',linewidth = 1) #left mid side
    ax1.plot(x_zone, y_sides, z_top, color = 'r',linewidth = 2) #top side
    ax1.plot(x_zone, y_sides, z_mid_top, color = 'r',linewidth = 1) #top middle
    ax1.plot(x_zone, y_sides, z_mid_bottom, color = 'r',linewidth = 1) #bottom middle
    ax1.plot(x_zone, y_sides, z_bottom, color = 'r',linewidth = 2) #bottom side

#Field
def field():
    #FoulLines
    x_Rline = [0,63.5]
    x_Lline = [0,63.5]
    y_Rline = [0,63.5]
    y_Lline = [0, -63.5]
    ax1.plot(x_Rline,y_Rline, color = '#696969')
    ax1.plot(x_Lline,y_Lline, color = '#696969')

    #home plate
    front_line_x = [1.417,1.417]
    front_line_y = [-.71,.71]
    right_side_x = [1.417,.707]
    right_side_y = [.71,.71]
    left_side_x = [1.417,.707]
    left_side_y = [-.71,-.71]
    right_diag_x = [.707,0]
    right_diag_y = [.71,0]
    left_diag_x = [.707,0]
    left_diag_y = [-.71,0]
    ax1.plot(front_line_x,front_line_y, color = 'k', linewidth = 2)
    ax1.plot(right_side_x,right_side_y,color = 'k', linewidth = 2)
    ax1.plot(left_side_x,left_side_y,color = 'k', linewidth = 2)
    ax1.plot(right_diag_x,right_diag_y,color = 'k', linewidth = 2)
    ax1.plot(left_diag_x,left_diag_y,color = 'k', linewidth = 2)

#draw pitch
def draw_pitch():
    x,y,z = np.loadtxt('test.txt', delimiter = '    ', unpack = True)
    ax1.plot(x,y,z,
        color = 'c',
        linewidth = 5.5,
        solid_capstyle = 'round')

0 个答案:

没有答案