3

I have this df_players:

 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   TableIndex      739 non-null    object 
 1   PlayerID        739 non-null    int64  
 2   GameWeek        739 non-null    int64  
 3   Date            739 non-null    object 
 4   Points          739 non-null    int64  
 5   Price           739 non-null    float64
 6   BPS             739 non-null    int64  
 7   SelectedBy      739 non-null    int64  
 8   NetTransfersIn  739 non-null    int64  
 9   MinutesPlayed   739 non-null    float64
 10  CleanSheet      739 non-null    float64
 11  Saves           739 non-null    float64
 12  PlayersBasicID  739 non-null    int64  
 13  PlayerCode      739 non-null    object 
 14  FirstName       739 non-null    object 
 15  WebName         739 non-null    object 
 16  Team            739 non-null    object 
 17  Position        739 non-null    object 
 18  CommentName     739 non-null    object 

And I'm using this function, with quantile() (value passed by variable 'cut'), to plot the distribution of players:

def jointplot(X, Y, week=None, title=None,
              positions=None, height=6,
              xlim=None, ylim=None, cut=0.015,
              color=CB91_Blue, levels=30, bw=0.5, top_rows=100000):

    if positions == None:
        positions = ['GKP','DEF','MID','FWD']

    #Check if week is given as a list
    if week == None:
        week = list(range(max(df_players['GameWeek'])))

    if type(week)!=list:
        week = [week]

    df_played = df_players.loc[(df_players['MinutesPlayed']>=45)
                              &(df_players['GameWeek'].isin(week))
                              &(df_players['Position'].isin(positions))].head(top_rows)   

    if xlim == None:
        xlim = (df_played[X].quantile(cut),
                df_played[X].quantile(1-cut))

    if ylim == None:
        ylim = (df_played[Y].quantile(cut),
                df_played[Y].quantile(1-cut))

    sns.jointplot(X, Y, data=df_played,
                  kind="kde", xlim=xlim, ylim=ylim,
                  color=color, n_levels=levels,
                  height=height, bw=bw);

    plt.suptitle(title,fontsize=18);
    plt.show()

call:

jointplot('Price', 'Points', positions=['FWD'],
          color=color_list[3], title='Forwards')

this plots:

enter image description here

where:

xlim = (4.5, 11.892999999999995)
ylim = (1.0, 13.0)

As far as I'm concerned, these x and y limits allow me, using the range of quantile value (cut),(1-cut), to zoom into an area of datapoints.


QUESTION

Now I would like to get player 'WebName' for players within a certain area, like so:

enter image description here

Ater ploting I can chose a target area above and define the range, roughly, passing xlim and ylim:

jointplot('Price', 'Points', positions=['FWD'], 
          xlim=(5.5, 7.0), ylim=(11.5, 13.0),
          color=color_list[3], title='Forwards')

which zooms in the area in red above.

enter image description here


But how can I get players names inside that area?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

1 Answers1

0

You can just select the portion of the players dataframe based on the bounds in the plot:

selected = df_players[
    (df_players.Points >= points_lbound)
    & (df_players.Points <= points_ubound)
    & (df_players.Price >= price_lbound)
    & (df_players.Price <= price_ubound)
]

The list of WebNames would then be selected.WebNames

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
  • thank you. you introduced new variables. could you just expand a little bit on lboud and ubound? are they the same as xlim and ylim on the code above? – 8-Bit Borges May 23 '20 at 22:13
  • Yep - sorry you can choose any values you'd like for these bounds. For example, where you use `xlim=(5.5, 7.0), ylim=(11.5, 13.0)`, you could use `5.5, 7.0, 11.5, 13.0` in place of `price_lbound, price_ubound, points_lbound, points_ubound`. – Michael Delgado May 23 '20 at 22:15
  • got it. for the sake of clarity, I think using `selected = df_players[ (df_players.Points >= ylim[0]) & (df_players.Points <= ylim[1]) & (df_players.Price >= xlim[0]) & (df_players.Price <= xlim[1]) ]` would be clearer. – 8-Bit Borges May 23 '20 at 22:17