Skip to content

Instantly share code, notes, and snippets.

@traeblain
Created October 2, 2012 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save traeblain/3816324 to your computer and use it in GitHub Desktop.
Save traeblain/3816324 to your computer and use it in GitHub Desktop.
xkcd "Hand drawn" plot
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from math import pi as pi
import numpy as np
import scipy.ndimage as ndimage
def distortPlot(y):
yOffset = y + y.max() / 100 * np.random.randn(len(y))
yOffSmoothed = ndimage.gaussian_filter(yOffset, sigma=1.75, order=0)
return yOffSmoothed
x = np.arange(0, 2 * pi, 0.05)
y = np.sin(x)
yp = np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
font = {'family': 'Comic Sans MS',
'weight': 'bold',
'size': 14}
linwidth = 3
xsin = distortPlot(x)
ysin = distortPlot(y)
xcos = xsin
ycos = distortPlot(yp)
horline = np.zeros(len(x)) + np.random.randn(len(x)) * .01
ax.plot(x, distortPlot(horline), lw=linwidth, color='black')
ax.plot(xsin, ysin, lw=linwidth * 3, color='white')
ax.plot(xsin, ysin, lw=linwidth)
ax.plot(xcos, ycos, lw=linwidth * 3, color='white')
ax.plot(xcos, ycos, lw=linwidth)
ax.axis([-0.1, (2 * pi) - 0.075, -1.05, 1.05])
ax.spines['bottom'].set_linewidth(0)
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_linewidth(0)
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='both', direction='in', width=3, length=10, pad=8)
verty = np.arange(ysin.min() - 0.05, ysin.max() + 0.05, 0.05)
vertline = np.zeros(len(verty)) + np.random.randn(len(verty)) * .05
ax.plot(distortPlot(vertline), verty, lw=linwidth, color='black')
ax.annotate('NICE CURVE', xy=(xsin[50], ysin[50]), xycoords='data',
xytext=(50, 30), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
lw=3,
connectionstyle="arc,angleA=180,armA=70,rad=20"),
)
plt.rc('font', **font)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment