Happy Thanksgiving!#
Hi all, for the upcoming US holiday, I wanted to share some some turkey with all of you! Actually though, which I managed to make a turkey in everyone’s favorite drawing tool matplotlib.
While I would not recommend doing this, it was a fun way to learn more about some of the lower level interfaces that matplotlib offers. I hope you all enjoy the holiday if you are celebrating!
The Original#
After scouring google images for hand turkeys, I finally found one that is worthy to recreate in matplotlib.
The Matplotlib Effort#
I have to admit, drawing this by hand with some crayons and a pencil would have
been much easier- but this was a fun adventure into some of the lower level
APIs that matplotlib
has to offer.
from matplotlib.pyplot import subplots, show, rc, imread
from matplotlib.patches import PathPatch, Circle, Rectangle, RegularPolygon, Path
from matplotlib import patheffects
from numpy import pi
from itertools import islice, tee, repeat, chain, repeat, zip_longest
import pathlib
nwise = lambda g, n=2: zip(*(islice(g, i, None) for i, g in enumerate(tee(g, n))))
nwise_longest = lambda g, n=2, fv=object(): zip_longest(*(islice(g, i, None) for i, g in enumerate(tee(g, n))), fillvalue=fv)
first = lambda g, n=1: zip(chain(repeat(True, n), repeat(False)), g)
last = lambda g, m=1, s=object(): ((y[-1] is s, x) for x, *y in nwise_longest(g, m+1, s))
polys = {
'ring': ('green', [(.3, .54), (.33, .8), (.37, .9), (.39, .8), (.39, .54), (.3, .54),]),
'middle': ('red', [(.4, .54), (.43, .8), (.47, .9), (.49, .8), (.49, .54), (.4, .54),]),
'index': ('orange', [(.5, .54), (.53, .8), (.57, .9), (.59, .8), (.59, .5), (.5, .54),]),
'palm': ('#a0522d', [(.3, .54), (.5, .7), (.6, .5), (.55, .3), (.7, .5), (.8, .55), (.8, .5),
(.6, .2), (.5, .15), (.45, .15), (.37, .15), (.35, .18), (.32, .2),
(.28, .4), (.28, .4), (.3, .54),],),
}
patches = {
'eye': Circle((.75, .49), radius=.01, facecolor='black'),
'beak': RegularPolygon((.79, .51), numVertices=3, radius=.03, orientation=pi/3, facecolor='orange', zorder=-1),
'snood': RegularPolygon((.74, .41), numVertices=3, radius=.03, orientation=0, facecolor='red', zorder=-1),
'hat': Rectangle((.67, .49), width=.1, height=.05, angle=25, facecolor='black'),
'brim': Rectangle((.64, .47), width=.16, height=.01, angle=25, facecolor='black'),
'buckle': Rectangle((.70, .515), width=.03, height=.03, angle=25, facecolor='goldenrod'),
}
lines = {
'left leg': ([.4, .4], [.05, .15]),
'left hallux': ([.38, .4], [.05, .07]),
'left pinky': ([.42, .4], [.05, .07]),
'right leg': ([.48, .48], [.05, .15]),
'right hallux': ([.46, .48], [.05, .07]),
'right pinky': ([.5, .48], [.05, .07]),
}
rc('axes.spines', top=False, left=False, bottom=False, right=False)
rc('xtick', bottom=False, labelbottom=False)
rc('ytick', left=False, labelleft=False)
fig, ax = subplots(dpi=140, figsize=(8, 4), gridspec_kw={'bottom': .1})
fig.set_facecolor('white')
for col, verts in polys.values():
codes = [Path.MOVETO if is_first else Path.CLOSEPOLY if is_last else Path.CURVE3 for is_first, (is_last, _) in first(last(verts))]
path = Path(verts, codes)
ax.add_patch(PathPatch(path, facecolor=col))
for p in patches.values():
ax.add_artist(p)
for xs, ys in lines.values():
ax.plot(xs, ys, zorder=-1, color='black', lw=3)
# Title inside of `Axes`
ax.text(
.5, .9, 'Happy Thanksgiving from', transform=ax.get_xaxis_transform(),
ha='center', size=36, color='#b8860b',
font=pathlib.Path('../_static/Interstate/Interstate Regular.otf'),
path_effects=[patheffects.withSimplePatchShadow()]
)
# Add Logo
logo_ax = fig.add_axes([0, 0, 1, ax.get_gridspec().get_subplot_params().bottom])
image_data = imread('../_static/images/DUTC_Logo_Horizontal_1001x61.png')
logo_ax.set_anchor('S')
logo_ax.imshow(image_data)
# Add personal watermark
ax.text(.64, .15, 'Cameron Riddell @RiddleMeCam', color='lightgray')
ax.set_aspect(1/2, anchor='C')
Wrap Up#
Not too bad! If you’re curious about some resources I used to create this hand turkey, check them out on the documentation pages:
If you observe Thanksgiving, I hope you have a great holiday. Talk to you all again next week!