Home > Mobile >  Newbie that needs help in changing Scale Axis in Python
Newbie that needs help in changing Scale Axis in Python

Time:10-05

I have a graph that is plotted on the following Axis X-axis going from -200 to 1300 [-200,1300] Y-Axis going from -800 to 800. [-800,800]

The values that I want to scatter range from X-Axis [0,735] y-Axis [0,437]

May I kindly ask you if there's any library/function that does that? I want to scale/adjust the scale of the points i'd like to scatter ([0,737], [0,437])

I am trying to scatter some points on the draw_court() function that is mentioned here: https://g-giasemidis.medium.com/create-euroleague-shot-charts-in-python-7ba4aa574644.

def draw_court(ax=None, color='black', lw=1, outer_lines=True):
    """
    FIBA basketball court dimensions:
    https://www.msfsports.com.au/basketball-court-dimensions/
    It seems like the Euroleauge API returns the shooting positions
    in resolution of 1cm x 1cm.
    """
    # If an axes object isn't provided to plot onto, just get current one
    if ax is None:
        ax = plt.gca()

    # Create the various parts of an NBA basketball court

    # Create the basketball hoop
    # Diameter of a hoop is 45.72cm so it has a radius 45.72/2 cms
    hoop = Circle((0, 0), radius=45.72 / 2, linewidth=lw, color=color,
                  fill=False)

    # Create backboard
    backboard = Rectangle((-90, -157.5   120), 180, -1, linewidth=lw,
                          color=color)

    # The paint
    # Create the outer box of the paint
    outer_box = Rectangle((-490 / 2, -157.5), 490, 580, linewidth=lw,
                          color=color, fill=False)
    # Create the inner box of the paint, widt=12ft, height=19ft
    inner_box = Rectangle((-360 / 2, -157.5), 360, 580, linewidth=lw,
                          color=color, fill=False)

    # Create free throw top arc
    top_free_throw = Arc((0, 580 - 157.5), 360, 360, theta1=0, theta2=180,
                         linewidth=lw, color=color, fill=False)
    # Create free throw bottom arc
    bottom_free_throw = Arc((0, 580 - 157.5), 360, 360, theta1=180, theta2=0,
                            linewidth=lw, color=color, linestyle='dashed')
    # Restricted Zone, it is an arc with 4ft radius from center of the hoop
    restricted = Arc((0, 0), 2 * 125, 2 * 125, theta1=0, theta2=180,
                     linewidth=lw, color=color)

    # Three point line
    # Create the side 3pt lines
    corner_three_a = Rectangle((-750   90, -157.5), 0, 305, linewidth=lw,
                               color=color)
    corner_three_b = Rectangle((750 - 90, -157.5), 0, 305, linewidth=lw,
                               color=color)
    # 3pt arc - center of arc will be the hoop, arc is 23'9" away from hoop
    # I just played around with the theta values until they lined up with the
    # threes
    three_arc = Arc((0, 0), 2 * 675, 2 * 675, theta1=12, theta2=167.5,
                    linewidth=lw, color=color)

    # Center Court
    center_outer_arc = Arc((0, 1400-157.5), 2 * 180, 2 * 180, theta1=180,
                           theta2=0, linewidth=lw, color=color)

    # List of the court elements to be plotted onto the axes
    court_elements = [hoop, backboard, outer_box, inner_box,
                      restricted, top_free_throw, bottom_free_throw,
                      corner_three_a, corner_three_b, three_arc,
                      center_outer_arc]
    if outer_lines:
        # Draw the half court line, baseline and side out bound lines
        outer_lines = Rectangle((-750, -157.5), 1500, 1400, linewidth=lw,
                                color=color, fill=False)
        court_elements.append(outer_lines)

    # Add the court elements onto the axes
    for element in court_elements:
        ax.add_patch(element)

I have also switched the court to the other side (switched X and Y Axis)

Thanks

CodePudding user response:

Assuming that you successfully called the draw_court() function and captured the return value in the variable ax, you can do following to adjust the scale to your preferred range:

ax = draw_court(ax)
ax.set(ylim=(0, 437), xlim=(0, 737))

CodePudding user response:

Perhaps I've mis-understood your question.. Could you not use the random module ?


import random

# set these to whatever your limits are, they can be adjusted with change of scale
XMIN = 0
XMAX = 735

# scale being whatever scale. Perhaps you'll have to make a call
# to int() if scale is a fraction 

xco_ord = random.randint((XMIN * scale), (XMAX * scale))

# and do the same for y

  • Related