|
From: Paul I. <piv...@gm...> - 2011-02-15 01:25:38
|
jules hummon, on 2011-02-14 07:23, wrote:
> Feel free to 'save and run', pass along, or ignore.
> This was my valentine's day present today.
Thank you for sharing! I took the liberty of livening it up for
my sweetheart.
#---------------------------------------------------
# hohumheartbeat.py - a more lively hohumheart.py ;)
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = plt.figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#ffffff')
ax2 = fig.add_axes([0.45, .9, 0.1, 0.1], polar=True, axisbg='#ffffff')
ax.set_rmax(2.0)
ax2.set_rmax(4.0)
ax2.spines['polar'].set_visible(False)
ax2.patch.set_alpha(.3)
for a in ax,ax2:
a.set_xticks([])
a.set_yticks([])
ax.grid(False)
theta = np.linspace(0,1,100)*np.pi*2
r = 1*(1-np.cos(theta))
ncards = 5
step = 2*np.pi/ncards
pdict = dict(fc='r',ec='k', alpha=.5)
for ii in range(ncards):
tr = np.column_stack((theta+ii*step, r))
ax.add_patch(Polygon(tr, **pdict))
# from WolframMath "Heart Curve"
r2 = 2 - 2*np.sin(theta)
r2 += np.sin(theta) * np.sqrt(np.abs(np.cos(theta))) / (np.sin(theta)+1.4)
tr2 = np.column_stack((theta, r2))
ax2.add_patch(Polygon(tr2,**pdict)) # heart
kwargs = dict(transform=ax2.transAxes, va='center', fontsize=20)
ax2.text(0,.45,"I",ha='right',**kwargs)
ax2.text(1,.4,"you",ha='left',**kwargs)
beat = np.tan(np.linspace(0, np.pi*2, 100))
beat[0:20] = beat[60:80]
beat[-20:] = beat[20:40]
beat -= beat.min()
beat /= beat.max()
i=0; beatlen = len(beat)-1;
def heartbeat(e):
global i;
i = (i+1) % (beatlen)
y = beat[i]
ax2.set_rmax(y*4. + 4.)
ax2.draw(ax.figure.canvas.get_renderer())
ax2.figure.canvas.blit(ax2.bbox)
#plt.draw() #use if the last two lines cause trouble
# even your mouse movements make my heart skip a bit!
cid = ax.figure.canvas.mpl_connect('idle_event', heartbeat)
print "ax.figure.canvas.mpl_disconnect(%d)" %cid
print "#run the line above to 'flatline' (stop heartbeat)"
plt.show()
best,
--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7
|