Skip to content

Instantly share code, notes, and snippets.

@beefy
Created September 3, 2018 15:07
Show Gist options
  • Save beefy/e6ecd16fe7f57cc804924a2b31c68406 to your computer and use it in GitHub Desktop.
Save beefy/e6ecd16fe7f57cc804924a2b31c68406 to your computer and use it in GitHub Desktop.
use matplotlib on lichess data
import matplotlib.pyplot as plt
import datetime
import numpy as np
from datetime import datetime
games = []
rec= {}
keys = [
"[Event ","[Site ","[Date ","[Round ","[White ","[Black ",
"[Result ","[UTCDate ","[UTCTime ","[WhiteElo ","[BlackElo ",
"[Variant ","[TimeControl ","[ECO ","[Termination ",
"[WhiteRatingDiff ","[BlackRatingDiff "
]
# parse pgn data into dict
parse = lambda key, line: line.replace(key,"").replace('"',"").replace("]","").replace("\n","")
for line in open("lichess_beefybeefy_2018-09-03.pgn","rb"):
for key in keys:
if key in line:
rec[key.replace("[","").replace(" ","")] = parse(key,line)
if not any([key in line for key in keys]) and len(rec.keys()) > 0:
games.append(rec)
rec = {}
has_key = lambda key: key in game.keys()
has_keys = lambda keys: all([has_key(key) for key in keys])
# rating by date and variant
ret = {}
plots = {}
for game in games:
if has_keys(["UTCDate","UTCTime","White","Black","WhiteElo","BlackElo","Event"]):
if "Rated" in game["Event"] and "tournament" not in game["Event"]:
if game["Event"] not in ret.keys():
ret[game["Event"]] = {}
plots[game["Event"]] = {"x":[],"y":[]}
date_str = game["UTCDate"] + " " + game["UTCTime"]
date = datetime.strptime(date_str,'%Y.%m.%d %H:%M:%S')
if game["White"] == "beefybeefy":
ret[game["Event"]][date] = game["WhiteElo"]
else:
ret[game["Event"]][date] = game["BlackElo"]
plots[game["Event"]]["x"].append(date)
plots[game["Event"]]["y"].append(ret[game["Event"]][date])
for variant in plots.keys():
x = np.array(plots[variant]["x"])
y = np.array(plots[variant]["y"])
plt.plot(x,y)
# add legend
legend = [key.replace("Rated ","").replace(" game","") for key in plots.keys()]
plt.legend(legend,loc="upper left")
# add horizontal lines
ax = plt.axes()
ax.yaxis.grid()
# size window
plt.gcf().set_size_inches(20, 10, forward=True)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment