public
Created

  • Download Gist
matplotlib_dataset.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.