Skip to content

Instantly share code, notes, and snippets.

@barrysmyth
Created May 25, 2020 17:22
Show Gist options
  • Save barrysmyth/dea6595c6497055c90930fef143b2275 to your computer and use it in GitHub Desktop.
Save barrysmyth/dea6595c6497055c90930fef143b2275 to your computer and use it in GitHub Desktop.
sns.set_context('talk')
fig, ax = plt.subplots(figsize=(15,8))
# Plot the segments as a stacked bar at the x coordinate.
def plot_segments(ax, x, segments, width=0.7, min_y=0, colour_dict=colour_dict):
current_y = min_y
for segment in segments:
# Plot a single bar at x with height=segment and its base at current_y
# Colour the bar based on the colour code for x
ax.bar([x], [segment], width, bottom=current_y, color=colour_dict[x])
# Update current_y so that the next segment is plotted on top.
current_y += segment
# Plot each of the country's segments using apply.
weeklies.apply(lambda segments: plot_segments(ax, segments.name, segments), axis=1)
# Rotate the x-axis labels for a better fit.
[label.set_rotation(90) for label in ax.get_xticklabels()]
ax.set_xlim(-1, len(weeklies))
ax.set_ylabel('Total Lockdown Mobility Drop')
fig.tight_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment