Guest User

Untitled

a guest
Jan 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. from plotnine import ggplot, annotate, aes, geom_text, geom_point, scale_size_manual, scale_shape_manual
  2. from plotnine import scale_colour_manual, ylim, xlim, coord_fixed, theme_void
  3. from plotnine.animation import PlotnineAnimation
  4. import pandas as pd
  5. import numpy as np
  6. import warnings
  7. from tqdm import tqdm
  8.  
  9.  
  10. def visualize_play(play_data, path=None):
  11. # General field boundaries
  12. xmin = 0
  13. xmax = 160 / 3.
  14. # hash_right = 38.35
  15. hash_left = 12
  16. # hash_width = 3.3
  17. ymin = max(round(np.nanmin(play_data.x) - 10, -1), 0)
  18. ymax = min(round(np.nanmax(play_data.x) + 10, -1), 120)
  19. df_x_hash, df_y_hash = np.meshgrid([0, 23.36667, 29.96667, xmax], list(range(10, 110 + 1)))
  20. df_hash = pd.DataFrame({'x': df_x_hash.flatten(), 'y': df_y_hash.flatten()})
  21. df_hash['label'] = '_'
  22.  
  23. df_hash = df_hash[(df_hash.y % 5 != 0) & (df_hash.y < ymax) & (df_hash.y > ymin)].reset_index(drop=True)
  24.  
  25. df_hash_left = df_hash[df_hash['x'] < 55 / 2.]
  26. df_hash_right = df_hash[df_hash['x'] > 55 / 2.]
  27.  
  28. # There are some odd warnings thrown, this takes care of them.
  29. with warnings.catch_warnings():
  30. warnings.simplefilter("ignore")
  31. play_data.is_copy = False
  32. pd.set_option('chained_assignment', None)
  33. plots = (plot(k, df_hash_left, df_hash_right, ymin, ymax, xmin, xmax, hash_left) for k in tqdm(play_data.groupby('frame.id')))
  34.  
  35. ani = PlotnineAnimation(plots, interval=100, repeat_delay=500)
  36.  
  37. # Save it out if a path is provided
  38. if path:
  39. ani.save(path)
  40. return ani
  41.  
  42.  
  43. def plot(data, df_hash_left, df_hash_right, ymin, ymax, xmin, xmax, hash_left):
  44. df_frame = data[1]
  45.  
  46. p = (ggplot() +
  47. geom_point(df_frame, aes(x='plot_x', y='plot_y', colour='team', group='nflId', pch='team', size='team'),
  48. inherit_aes=False, na_rm=True) +
  49. geom_text(df_frame, aes(x='plot_x', y='plot_y', label='jerseyNumber'), colour="white", size=9,
  50. inherit_aes=False, na_rm=True) +
  51. geom_text(df_hash_left, aes(label='label', x='x', y='y'), nudge_x=0.9, nudge_y=.8) +
  52. geom_text(df_hash_right, aes(label='label', x='x', y='y'), nudge_x=-1.1, nudge_y=.8) +
  53. annotate("segment", x=xmin, y=list(range(max(10, int(ymin)), min(int(ymax), 110) + 5, 5)),
  54. xend=xmax, yend=list(range(max(10, int(ymin)), min(int(ymax), 110) + 5, 5))) +
  55. annotate("segment", x=[xmin, xmin, xmax, xmax], y=[ymin, ymax, ymax, ymin],
  56. xend=[xmin, xmax, xmax, xmin], yend=[ymax, ymax, ymin, ymin]) +
  57. annotate("text", x=[hash_left] * 11, y=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110],
  58. label=["G ", 10, 20, 30, 40, 50, 40, 30, 20, 10, " G"], angle=270, size=10) +
  59. annotate("text", x=[xmax - hash_left] * 11, y=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110],
  60. label=["G ", 10, 20, 30, 40, 50, 40, 30, 20, 10, " G"], angle=270, size=10) +
  61. scale_size_manual(values=[6, 4, 6], guide=False) +
  62. scale_shape_manual(values=[19, 16, 19], guide=False) +
  63. scale_colour_manual(values=["#e31837", "#654321", "#002244"], guide=False) +
  64. ylim(ymin, ymax) +
  65. xlim(xmin, xmax) +
  66. coord_fixed() +
  67. theme_void())
  68. return p
Add Comment
Please, Sign In to add comment