Optimizing and Improving Python Code that Models the 3D Trajectory of a Thrown Baseball












0














I've been working on learning Python as I feel I need to expand my knowledge of programming languages. After reading up and watching a few tutorials, I came up with a basic program that models a baseball thrown from the pitchers mound to home plate with numpy and maplotlib. As i have just began learning python, I do not believe my code is all that great.



The first thing I ask is and suggestions for improvements. I would like to optimize this before I start adding to it. Any links provided or just general help would be great.



Second, is using matplotlib the best way to go about this is or is there something else you would suggest?



Third, I would like to add a pitching mound, just a simple semisphere, into the program, could someone point me in the right direction to go on with that?



Thanks, any and all help/guidance/comments is appreciated!



I will include the functions in my code that I would like to improve.



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









share|improve this question



























    0














    I've been working on learning Python as I feel I need to expand my knowledge of programming languages. After reading up and watching a few tutorials, I came up with a basic program that models a baseball thrown from the pitchers mound to home plate with numpy and maplotlib. As i have just began learning python, I do not believe my code is all that great.



    The first thing I ask is and suggestions for improvements. I would like to optimize this before I start adding to it. Any links provided or just general help would be great.



    Second, is using matplotlib the best way to go about this is or is there something else you would suggest?



    Third, I would like to add a pitching mound, just a simple semisphere, into the program, could someone point me in the right direction to go on with that?



    Thanks, any and all help/guidance/comments is appreciated!



    I will include the functions in my code that I would like to improve.



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









    share|improve this question

























      0












      0








      0







      I've been working on learning Python as I feel I need to expand my knowledge of programming languages. After reading up and watching a few tutorials, I came up with a basic program that models a baseball thrown from the pitchers mound to home plate with numpy and maplotlib. As i have just began learning python, I do not believe my code is all that great.



      The first thing I ask is and suggestions for improvements. I would like to optimize this before I start adding to it. Any links provided or just general help would be great.



      Second, is using matplotlib the best way to go about this is or is there something else you would suggest?



      Third, I would like to add a pitching mound, just a simple semisphere, into the program, could someone point me in the right direction to go on with that?



      Thanks, any and all help/guidance/comments is appreciated!



      I will include the functions in my code that I would like to improve.



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









      share|improve this question













      I've been working on learning Python as I feel I need to expand my knowledge of programming languages. After reading up and watching a few tutorials, I came up with a basic program that models a baseball thrown from the pitchers mound to home plate with numpy and maplotlib. As i have just began learning python, I do not believe my code is all that great.



      The first thing I ask is and suggestions for improvements. I would like to optimize this before I start adding to it. Any links provided or just general help would be great.



      Second, is using matplotlib the best way to go about this is or is there something else you would suggest?



      Third, I would like to add a pitching mound, just a simple semisphere, into the program, could someone point me in the right direction to go on with that?



      Thanks, any and all help/guidance/comments is appreciated!



      I will include the functions in my code that I would like to improve.



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






      python python-3.x numpy matplotlib data-modeling






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 0:06









      jmaschino56

      11




      11
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952227%2foptimizing-and-improving-python-code-that-models-the-3d-trajectory-of-a-thrown-b%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952227%2foptimizing-and-improving-python-code-that-models-the-3d-trajectory-of-a-thrown-b%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'