Build. Hack. Play. It's just what we do.
Build. Hack. Play. It's just what we do.

So, everyone loves xkcd, right? Well, if you've ever wanted to make an XKCD style graph, you can with the help of Python's matplotlib library.

Illustration for article titled XKCD style plots
import numpy as np
import matplotlib.pyplot as plt

plt.xkcd()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xticks([])
plt.yticks([])
ax.set_ylim([-30, 10])
data = np.ones(100)
data[70:] -= np.arange(30)
plt.annotate(
'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
plt.plot(data)
plt.xlabel('time')
plt.ylabel('my overall health')
fig.savefig("bacon.png")
plt.show()

Advertisement

Share This Story

Get our newsletter