Skip to content

Instantly share code, notes, and snippets.

@eldondev
Last active August 29, 2015 14:09
Show Gist options
  • Save eldondev/1f6f707f249552cf1a5a to your computer and use it in GitHub Desktop.
Save eldondev/1f6f707f249552cf1a5a to your computer and use it in GitHub Desktop.
Playing with amazon ec2 spot instance pricing, ipython notebook, and matplotlib
# coding: utf-8
# In[23]:
get_ipython().magic('pylab')
import boto.ec2
from pandas import DataFrame
pre_data = {}
for region in boto.ec2.regions():
if not region.name.startswith('cn') and not 'gov' in region.name:
pre_data[region.name] = region.connect().get_spot_price_history(instance_type='t1.micro')
from collections import defaultdict
data = defaultdict(lambda :[])
for price_history in pre_data.values():
for price in price_history:
data[price.availability_zone].append(price)
# In[24]:
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
for i in range(len(tableau20)):
r, g, b = tableau20[i]
tableau20[i] = (r / 255., g / 255., b / 255.)
def from_utc(utcTime,fmt="%Y-%m-%dT%H:%M:%S.%fZ"):
"""
Convert UTC time string to time.struct_time
"""
# change datetime.datetime to time, return time.struct_time type
return datetime.datetime.strptime(utcTime, fmt)
# In[24]:
# In[33]:
def make_figure():
yuk = list(data.items())
figure(figsize=(12, 14))
for rank, (k,v) in enumerate(yuk):
print(k)
clf()
ax = subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
ylim(0,0.03)
j = [(from_utc(t.timestamp), t.price) for t in v]
j.sort()
x, y = [i[0] for i in j], [i[1] for i in j]
plot(x,y, lw=0.5, color=tableau20[rank % 20])
text(x[-1],0.02,k, fontsize=17, ha="center")
savefig("%s-%s.png" % (k,str(x[-1])), bbox_inches="tight");
# In[34]:
make_figure()
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment