Skip to content

Instantly share code, notes, and snippets.

@treycausey
Last active February 19, 2019 12:23
Show Gist options
  • Save treycausey/97694490d2ca368e7141 to your computer and use it in GitHub Desktop.
Save treycausey/97694490d2ca368e7141 to your computer and use it in GitHub Desktop.
Slopegraphs in matplotlib
# Slopegraphs in matplotlib
# Trey Causey (@treycausey)
# Problems arise when equal values occur
# within the same time unit
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
units = ['A', 'B', 'C', 'D']
times = ['time1', 'time2', 'time3']
df = pd.DataFrame(index=units, columns=times)
df.time1 = [4, 5, 6, 7]
df.time2 = [5, 4, 7, 6]
df.time3 = [5, 4, 6, 7]
ymin = min(df.min())
ymax = max(df.max())
num_times = df.shape[1]
num_units = df.shape[0]
plt.xlim(0, num_units)
plt.ylim(ymin - 1, ymax + 1)
plt.axis('off')
for time in range(num_times):
for unit in range(num_units):
plt.text(time + 1,
df.values[unit][time] - .05,
'{} : {}'.format(df.index[unit], df.values[unit][time]),
horizontalalignment='center',
verticalalignment='baseline')
num_lines = range(1, num_times)
for l in num_lines:
for row in df.values:
plt.plot([l + .25, l + .75], [row[l - 1], row[l]])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment