Skip to content

Instantly share code, notes, and snippets.

@elena-roff
Last active January 29, 2019 11:04
Show Gist options
  • Save elena-roff/cb0254576bc589b3da18ded49e1ac11b to your computer and use it in GitHub Desktop.
Save elena-roff/cb0254576bc589b3da18ded49e1ac11b to your computer and use it in GitHub Desktop.
Plot a graph that draws subplots for different categories of the count of items per time intervals
label_size = 30
plt.rcParams['xtick.labelsize'] = label_size
plt.figure(figsize=(100, 250))
# put on one date plain
df['departure_time_utc'] = df['departure_time_utc'].apply(lambda dt: dt.replace(year=2018, month=11, day=20))
# group by category
grouped = df.groupby('departure_and_destination')
# and pick the most frequent categories
grouped = grouped.filter(lambda x: x.shape[0] >= 100).groupby('departure_and_destination')
num_subplots = len(grouped)
for i_group, (dd_name, dd_values) in enumerate(grouped):
ax = plt.subplot(round(num_subplots/3), 3, i_group + 1) # nrows, ncols, axes position
dd_values.set_index('departure_time_utc').groupby(pd.Grouper(freq='30Min'))['booking_id'].count().plot(ax=ax)
ax.set_title(dd_name, fontsize=50)
plt.tight_layout()
# save to PDF
plt.savefig('outbound_dep_time_per_dd_pair.pdf')
plt.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment