Plotting football with Python and Plotly

Some time ago I got a question from Tom Worville, at http://analyticsfc.co.uk/ if I could share how I create my plots. As I have mentioned before I use Plotly. In my opinion a superb visualization tool with a great api and a sound user policy. Being a Python programmer their eminent python library is of course extra appreciated.

Here is how I create a football pitch with Plotly. Meet footballpitchplot!

I have made a small Python library that draws a football pitch on a Plotly graph. It can then then add/plot events on top of the pitch.

I have put the code up on github. So one way of using the library from github would of course be to use it as a submodule, or fork it, or maybe just take inspiration and implement it in another programming language.

But, I have put the library on pypi as well so that would be the easiest way of using the library. Given you have pip installed it would just be as easy as doing:

pip install footballpitchplot

That will install the library as well as Plotly. To set up Plotly one will need to create an account and have some credentials created locally. Instructions here.

Once this is done the following code will produce a plot of some events on top of a football pitch in Plotly.

from pitchplotter.plotter import Plotter

plotter = Plotter("Demo")

#create array as follows: [xpos, ypos, 'event name', event size]
demo_arr = [
    [25,50,'Event 1', 34],
    [35,41,'Event 2', 14],
    [20,33,'Event 3', 23],
    [10,52,'Event 4', 56],
    [45,60,'Event 5', 9]
]

plotter.add_events(demo_arr, "Demo events")

plotter.plot()

Thats all really!

Published by

Ola Lidmark Eriksson

Football analyst/programmer https://blog.stryktipsetisistastund.se/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s