Skip to content

Instantly share code, notes, and snippets.

@camriddell
Created November 14, 2022 23:23
Show Gist options
  • Save camriddell/2f6efbb8cd85c770c9d3bef001652471 to your computer and use it in GitHub Desktop.
Save camriddell/2f6efbb8cd85c770c9d3bef001652471 to your computer and use it in GitHub Desktop.
from pandas import DataFrame
from numpy.random import default_rng
from numpy import linspace, sin, cos
from matplotlib.pyplot import (
rcdefaults, rc, show, ion, figure,
pause, ioff, rcParams, waitforbuttonpress,
imread
)
from matplotlib.ticker import MultipleLocator
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
from matplotlib.lines import Line2D
from math import pi
rcdefaults()
rcParams['toolbar'] = 'None'
rc('font', size=30)
def clt(fig):
ax = fig.add_axes([0, 0.05, 1, .8])
img = imread('clt_viz.png')
ax.imshow(img)
ax.axis('off')
fig.text(.5, .95, 'Matplotlib, Not Just A Plotting Tool', ha='center', va='center', weight='bold')
fig.text(.5, .9, 'Thank You! For more viz follow me on Twitter @RiddleMeCam', size=20, ha='center', va='top')
fig.add_artist(Line2D([.2, .8], [.85, .85], lw=2, color='gainsboro'))
fig.canvas.draw_idle()
waitforbuttonpress()
def title(fig):
title = fig.text(.5, .6, 'Matplotlib, Not Just A Plotting Tool', ha='center', va='bottom', weight='bold')
x, y = title.get_position()
disclaimer = fig.text(.5, .4, '\n'.join([
'All images you see here were made in matplotlib.',
'No other packages were used.', 'No Seaborn, no NetworkX, nothing else',
'All plots were also made last night and this afternoon.',
]), ha='center', va='top', size=20)
fig.canvas.draw_idle()
waitforbuttonpress()
for y, rot in zip(linspace(y, 1, 75), linspace(0, -1080, 100)):
title.set_position((y, y))
title.set_rotation(rot)
fig.canvas.draw_idle()
pause(.005)
waitforbuttonpress()
def alt_title(fig):
title = fig.text(.5, .6, 'Matplotlib, Not Just A Plotting Tool', ha='center', va='center', weight='bold')
x, y = title.get_position()
disclaimer = fig.text(.5, .4, '\n'.join([
'Everything you saw here today was done in matplotlib',
'No PowerPoint, no Google Slides',
'Just Python & matplotlib'
]), ha='center', va='top')
fig.canvas.draw_idle()
waitforbuttonpress()
# for y, rot in zip(linspace(y, 1, 75), linspace(0, -1080, 360)):
# title.set_position((y, y))
# title.set_rotation(rot)
# fig.canvas.draw_idle()
# pause(.005)
def basic_plot(fig):
xs = linspace(0, 2 * pi, 100)
ax = fig.add_axes([.2, .2, .6, .6])
ax.set_title('The Most Basic Thing You Can Make?')
waitforbuttonpress()
ax.plot(xs, sin(xs), label='sin', lw=4)
ax.plot(xs, cos(xs), label='cos', lw=4)
ax.legend()
fig.canvas.draw_idle()
waitforbuttonpress()
def stickfigure(fig):
from matplotlib.pyplot import figure, show
from matplotlib.patches import Circle, Rectangle
fig.text(.5, .8, s='The Actual Most Basic Thing You Can Make', ha='center', va='center')
c = Circle((.5, .65), .1)
fig.add_artist(c)
waitforbuttonpress()
body_rect = Rectangle((.47, .65), .05, -.5)
fig.add_artist(body_rect)
waitforbuttonpress()
arms_rect = Rectangle((.35, .35), .3, .05)
fig.add_artist(arms_rect)
waitforbuttonpress()
lleg_rect = Rectangle((.49, .2), .25, .05, angle=225)
fig.add_artist(lleg_rect)
waitforbuttonpress()
rleg_rect = Rectangle((.46, .16), .25, .05, angle=-45)
fig.add_artist(rleg_rect)
fig.canvas.draw_idle()
waitforbuttonpress()
def complaint(fig):
title = fig.text(
.5, .6,
"If there isn't a surface level API call that does what you want",
ha='center', va='bottom'
)
fig.canvas.draw_idle()
waitforbuttonpress()
fig.text(.5, .45, "Keep Digging", ha='center', va='top', weight='bold', size=50)
fig.canvas.draw_idle()
waitforbuttonpress()
def embedding_more_meaning(fig):
rng = default_rng(0)
data = DataFrame(data=rng.uniform(-1, 1, size=(5, 6)), index=[*'ABCDE'])
ax = fig.add_axes([.2, .2, .6, .6])
img = ax.pcolormesh(
data.columns, data.index, data.values,
shading='nearest', vmin=-1, vmax=1, cmap='RdBu'
)
ax.set_title('Colormesh', fontsize='x-large')
ax.xaxis.set_minor_locator(MultipleLocator(.5))
ax.yaxis.set_minor_locator(MultipleLocator(.5))
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))
ax.tick_params(which='minor', length=0)
ax.set_title('Weak Channel Signal → Weak Message')
fig.canvas.draw_idle()
waitforbuttonpress()
scatter_data = data.stack()
y = ax.convert_yunits(scatter_data.index.get_level_values(0))
x = ax.convert_xunits(scatter_data.index.get_level_values(1))
sizes = img.norm(abs(scatter_data)) * .4
ax.clear()
ax.xaxis.set_minor_locator(MultipleLocator(.5))
ax.yaxis.set_minor_locator(MultipleLocator(.5))
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.set_yticks([0, 1, 2, 3, 4], labels=data.index)
collection = PatchCollection(
[Circle((xi, yi), radius=si) for xi, yi, si in zip(x, y, sizes)],
cmap=img.cmap, norm=img.norm, edgecolors='k', zorder=2
)
collection.set_array(scatter_data)
ax.add_collection(collection)
ax.grid(which='major')
ax.tick_params(which='minor', length=0)
ax.set(
title='Redundant Signal → Stronger Communication',
xlim=(0-.5, 5+.5),
ylim=(0-.5, 4+.5),
)
fig.canvas.draw_idle()
waitforbuttonpress()
def graph(fig):
fig.suptitle('You Can Visualize Networks…')
node_locs = {(.2, .5): [(.7, .7), (.7, .5), (.7, .3)]}
lines = []
ls = (0, (7, 10))
for parent, children in node_locs.items():
c = Circle(parent, .07)
fig.add_artist(c)
for ch in children:
c = Circle(ch, .05)
fig.add_artist(c)
x, y = [*zip(parent, ch)]
l = Line2D(x, y, color='k', ls=ls, lw=3, zorder=-1)
lines.append(l)
fig.add_artist(l)
fig.canvas.draw_idle()
waitforbuttonpress()
for l in lines:
offset, onoff = ls
for i in range(1, 10+1):
l.set_linestyle((offset-i, onoff))
fig.canvas.draw_idle()
pause(.05)
fig.canvas.draw_idle()
waitforbuttonpress()
def table(fig):
fig.text(.5, .95, 'You Can Even Make Tables…', ha='center', va='center')
axes = fig.subplots(5, 3, gridspec_kw={'hspace': 0, 'wspace': 0})
for ax in axes.flat:
ax.spines[['left', 'right', 'top', 'bottom']].set_visible(False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
for ax in axes[::2, :].flat:
ax.set_facecolor('gainsboro')
headers = [*'ABC']
for h, ax in zip(headers, axes[0, :].flat):
ax.spines['bottom'].set_visible(True)
ax.spines['bottom'].set_linewidth(2)
ax.text(.5, .5, h, ha='center', va='center')
xs = range(12)
for x, ax in zip(xs, axes[1:, :].flat):
ax.text(.5, .5, x, ha='center', va='center')
fig.canvas.draw_idle()
waitforbuttonpress()
def inspiring_message(fig):
msg = 'When you are free from a plotting API...'
title = fig.text(.5, .6, msg, ha='center', va='bottom')
fig.canvas.draw_idle()
waitforbuttonpress()
subtitle = fig.text(.5, .5, 'You are only limited by your creativity', ha='center', va='center', weight='bold')
fig.canvas.draw_idle()
waitforbuttonpress()
def clear(fig):
for artist in fig.get_children():
try:
artist.remove()
except NotImplementedError:
artist.set_visible(False)
fig = figure(dpi=144)
pause(.01)
main_slides = [
title, basic_plot, stickfigure, complaint, graph, table,
inspiring_message, embedding_more_meaning
]
for s in main_slides:
s(fig)
clear(fig)
wrapup_slides = [alt_title, clt]
for s in wrapup_slides:
s(fig)
clear(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment