import matplotlib.pyplot as plt | |
from random import randint | |
from time import time | |
DATA = [ | |
(i, randint(5,30), randint(5,30), randint(30,35), randint(1,5)) \ | |
for i in xrange(1, 401) | |
] | |
def mapValues(group): | |
ind, open_, close, high, low = group | |
if open_ > close: # if open is higher then close | |
height = open_ - close # heigth is drawn at bottom+height | |
bottom = close | |
yerr = (open_ - low, high - open_) | |
color = 'r' # plot as a white barr | |
else: | |
height = close - open_ # heigth is drawn at bottom+height | |
bottom = open_ | |
yerr = (close - low, high - close) | |
color = 'g' # plot as a black bar | |
return (ind, height, bottom, yerr, color) | |
# | |
# Test 1 | |
# | |
def test1(): | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
data = map(mapValues, DATA) | |
start = time() | |
for group in data: | |
ind, height, bottom, yerr, color = group | |
ax.bar(left=ind, height=height, bottom=bottom, yerr=zip(yerr), | |
color=color, ecolor='k', zorder=10, | |
error_kw={'barsabove': False, 'zorder': 0, 'capsize': 0}, | |
alpha=1) | |
return time()-start | |
# | |
# Test 2 | |
# | |
def test2(): | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
# plotData can be serialized | |
plotData = zip(*map(mapValues, DATA)) | |
ind, height, bottom, yerr, color = plotData | |
start = time() | |
ax.bar(left=ind, height=height, bottom=bottom, yerr=zip(*yerr), | |
color=color, ecolor='k', zorder=10, | |
error_kw={'barsabove': False, 'zorder': 0, 'capsize': 0}, | |
alpha=1) | |
return time()-start | |
def doTest(fn): | |
end = fn() | |
print "%s - Sec: %0.3f, ms: %0d" % (fn.__name__, end, end*1000) | |
if __name__ == "__main__": | |
doTest(test1) | |
doTest(test2) | |
# plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment