Skip to content

Commit

Permalink
plotly fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed Feb 2, 2024
1 parent 36daf9c commit c9cc4e4
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

DAYS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

YEAR_TO_SPLIT = datetime.datetime.now().year - 1
YEAR_TO_SPLIT_STR = str(YEAR_TO_SPLIT)

A = argparse.ArgumentParser(
description='Generates plots of crossword statistics from a CSV',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
Expand Down Expand Up @@ -124,18 +127,21 @@ def save_vln_plot(df, out_path, ymax):

def save_split_vln_plot(df, out_path, ymax):
"""
Splits the violin plot into pre-2021 and 2021+ sections to look
Splits the violin plot into pre- and post- YEAR_TO_SPLIT sections to look
at progress over time.
df: dataframe containing crossword times
out_path: filename to save plot to
ceiling: max y-value to show
"""
df['solve_time_m'] = df['solve_time_secs'] / 60.0
# TODO: should probably not hard-code 2021 and instead pass in a date.
df['In 2021'] = df['Solved datetime'] > datetime.datetime(2021, 1, 1)
ax = sns.violinplot(x="weekday", y="solve_time_m", hue='In 2021',
split=True, data=df, bw=.25, order=DAYS)
df['In ' + YEAR_TO_SPLIT_STR] = df['Solved datetime'] > datetime.datetime(YEAR_TO_SPLIT, 1, 1)
try:
ax = sns.violinplot(x="weekday", y="solve_time_m", hue='In ' + YEAR_TO_SPLIT_STR,
split=True, data=df, bw=.25, order=DAYS)
except:
# Happens if there is no data from last year
return

date = max(df['Solved datetime']).strftime("%b %d, %Y")
ax.set_title("%d NYT Crossword Solve Times by Day of Week as of %s" % (len(df), date))
Expand All @@ -147,7 +153,7 @@ def save_split_vln_plot(df, out_path, ymax):

ax.legend() # Seems to have the effect of removing the title of the legend
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, ["Before 2021", "2021"], loc="upper left")
ax.legend(handles, ["Before " + YEAR_TO_SPLIT_STR, YEAR_TO_SPLIT_STR + "+"], loc="upper left")

plt.savefig(out_path)
plt.close()
Expand All @@ -156,7 +162,7 @@ def save_split_vln_plot(df, out_path, ymax):
def generate(in_file, out_file, ceiling = None):
df = parse_data(in_file)

if ceiling is None:
if not ceiling:
# Pick an appropriate y-axis, balancing being robust to outliers vs. showing all data
ymax = df["solve_time_secs"].quantile(0.99) / 60
else:
Expand Down

0 comments on commit c9cc4e4

Please sign in to comment.