Skip to content

Instantly share code, notes, and snippets.

@barrysmyth
Last active May 26, 2020 08:45
Show Gist options
  • Save barrysmyth/09aaa614cbe4bfc0306615dce47bbfc3 to your computer and use it in GitHub Desktop.
Save barrysmyth/09aaa614cbe4bfc0306615dce47bbfc3 to your computer and use it in GitHub Desktop.
fig, ax = plt.subplots(figsize=(15,8))
# Plot the segments as a stacked bar for a given x coordinate.
# Each x coordinate value will correspond to a country in our dataset.
def plot_segments(ax, x, segments, width=0.7, min_y=0):
current_y = min_y # The base of each bar.
# Create a new segment for each weekly value and stack the segments.
for segment in segments:
# Plot a single bar at x with height=segment and its base at current_y
ax.bar([x], [segment], width, bottom=current_y, color='coral', linewidth=1)
# 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