#!/usr/bin/env python

"""
This example utlizes restore_region with optional bbox and xy
arguments.  The plot is continuously shifted to the left. Instead of
drawing everything again, the plot is saved (copy_from_bbox) and
restored with offset by the amount of the shift. And only newly
exposed area is drawn. This technique may reduce drawing time for some cases.
"""

import time

import gtk, gobject

import matplotlib
matplotlib.use('GTKAgg')

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pylab import *
dummyData = [(733486.55347222218, 1204.75, 1204.5, 1204.75, 1203.5, 107), (733486.5541666667, 1204.5, 1204.25, 1204.5, 1203.5, 72), (733486.5548611111, 1204.25, 1204.0, 1204.5, 1203.75, 116), (733486.5555555555, 1204.25, 1203.75, 1204.25, 1203.5, 94), (733486.55625000002, 1203.75, 1204.5, 1204.75, 1203.75, 199), (733486.55694444443, 1204.5, 1204.25, 1204.75, 1204.25, 70), (733486.55763888895, 1204.5, 1204.25, 1204.5, 1203.75, 211), (733486.55833333335, 1204.0, 1203.5, 1204.25, 1203.5, 172), (733486.55902777775, 1203.5, 1203.25, 1203.75, 1203.25, 153), (733486.55972222227, 1203.5, 1202.0, 1203.5, 1201.75, 384), (733486.56041666667, 1202.0, 1202.0, 1202.0, 1201.75, 238), (733486.56111111108, 1201.75, 1202.0, 1202.25, 1201.0, 323), (733486.5618055556, 1202.0, 1202.75, 1203.0, 1201.5,447), (733486.5625, 1202.75, 1205.0, 1205.75, 1202.75, 1364), (733486.5631944444, 1205.0, 1212.0, 1212.0, 1204.75, 4486), (733486.56388888892, 1212.0, 1210.75, 1214.5, 1209.75, 4153), (733486.56458333333, 1210.5, 1210.75, 1211.5, 1209.75, 1371), (733486.56527777773, 1211.0, 1209.75, 1212.5, 1209.75, 1398), (733486.56597222225, 1209.75, 1211.25, 1212.75, 1209.75, 1968), (733486.56666666665, 1210.75, 1209.5, 1210.75, 1207.75, 2513), (733486.56736111105, 1209.25, 1210.75, 1211.5, 1209.25, 1776), (733486.56805555557, 1210.25, 1211.75, 1212.0, 1209.25, 1537), (733486.56874999998, 1211.5, 1210.5, 1212.0, 1209.5, 1504), (733486.5694444445, 1210.5, 1210.5, 1211.5, 1209.75, 1329), (733486.5701388889, 1210.25, 1208.5, 1210.5, 1208.25, 2450), (733486.5708333333, 1208.5, 1206.75, 1208.75, 1206.5, 803), (733486.57152777782, 1206.75, 1207.25, 1208.25, 1206.0, 2403), (733486.57222222222, 1207.25, 1207.75, 1208.0, 1206.25, 1093), (733486.57291666663, 1207.75, 1208.25, 1209.25, 1207.25, 1220), (733486.57361111115, 1208.25, 1208.0, 1208.75, 1207.5, 785), (733486.57430555555, 1208.25, 1210.25, 1210.5, 1208.0, 733), (733486.57499999995, 1210.5, 1211.75, 1212.25, 1210.0, 1053), (733486.57569444447, 1211.75, 1211.5, 1212.75, 1211.5, 645), (733486.57638888888, 1212.0, 1212.75, 1215.25, 1211.75, 2054),(733486.57708333328, 1212.75, 1215.5, 1216.5, 1212.5, 1718), (733486.5777777778, 1215.5, 1216.75, 1218.25, 1215.0, 2180), (733486.5784722222, 1216.75, 1218.25, 1220.0, 1216.5, 1979), (733486.57916666672, 1218.0, 1218.75, 1219.0, 1217.5, 1215), (733486.57986111112, 1219.0, 1216.25, 1219.0, 1216.0, 1143), (733486.58055555553, 1216.25, 1214.25, 1217.5, 1214.25, 1563), (733486.58125000005, 1214.75, 1212.75, 1215.5, 1212.25, 1056), (733486.58194444445, 1212.75, 1213.75, 1214.0, 1211.5, 1599), (733486.58263888885, 1213.75, 1212.5, 1215.0, 1212.5, 775), (733486.58333333337, 1212.5, 1213.25, 1213.25, 1211.75, 1120), (733486.58402777778, 1213.25, 1213.0, 1214.0, 1213.0, 790), (733486.58472222218, 1213.0, 1212.5, 1213.5, 1212.25, 861), (733486.5854166667, 1212.25, 1213.25, 1213.25, 1211.75, 977), (733486.5861111111, 1213.25, 1212.25, 1213.25, 1211.75, 1077), (733486.5868055555, 1212.0, 1212.25, 1212.5, 1211.75, 574), (733486.58750000002, 1212.25, 1210.25, 1212.5, 1210.0, 1385), (733486.58819444443, 1210.5, 1208.0, 1210.5, 1208.0, 747), (733486.58888888895, 1208.0, 1207.5, 1208.5, 1207.0, 1296), (733486.58958333335, 1207.75, 1209.75, 1210.0, 1207.5, 1081), (733486.59027777775, 1210.0, 1209.25, 1210.25, 1208.5, 1113), (733486.59097222227, 1209.0, 1209.75, 1210.75, 1208.75, 1486), (733486.59166666667, 1209.75, 1209.5, 1210.25, 1208.0, 795), (733486.59236111108, 1209.5, 1208.5, 1209.75, 1208.5, 472), (733486.5930555556,1208.5, 1208.0, 1210.0, 1207.25, 1593), (733486.59375, 1208.0, 1208.75, 1208.75, 1207.5, 574), (733486.5944444444, 1208.5, 1210.25, 1210.25, 1208.25, 631), (733486.59513888892, 1210.0, 1210.5, 1211.0, 1209.75, 1317), (733486.59583333333, 1210.25, 1210.0, 1210.5, 1209.0, 1033), (733486.59652777773, 1209.75, 1208.0, 1210.0, 1207.5, 988), (733486.59722222225, 1208.0, 1206.75, 1208.5, 1206.5, 872), (733486.59791666665, 1206.75, 1207.0, 1207.25, 1206.5, 490), (733486.59861111105, 1207.0, 1208.5, 1208.5, 1206.75, 1147),(733486.59930555557, 1208.25, 1207.5, 1208.5, 1207.25, 549), (733486.59999999998, 1207.75, 1207.0, 1208.75, 1206.75, 606), (733486.6006944445, 1207.0, 1209.0, 1209.5, 1206.75, 683), (733486.6013888889, 1208.75, 1209.75, 1210.25, 1208.25, 1164), (733486.6020833333, 1210.0, 1211.25, 1212.0, 1209.25, 1190), (733486.60277777782, 1211.0, 1211.75, 1212.0, 1210.5, 758), (733486.60347222222, 1211.5, 1211.75, 1212.0, 1210.75, 642), (733486.60416666663, 1211.75, 1213.75, 1214.0, 1211.25, 1129), (733486.60486111115, 1213.75,1213.75, 1213.75, 1212.5, 1147), (733486.60555555555, 1213.5, 1212.5, 1213.75, 1212.5, 493), (733486.60624999995, 1212.75, 1212.75, 1213.0, 1211.5, 706), (733486.60694444447, 1212.75, 1212.25, 1212.75, 1211.75, 429), (733486.60763888888, 1212.25, 1212.25, 1212.75, 1211.75, 544), (733486.60833333328, 1212.5, 1211.75, 1212.5, 1211.25, 595), (733486.6090277778, 1211.75, 1210.75, 1211.75, 1210.0, 706), (733486.6097222222, 1210.75, 1210.75, 1211.75, 1210.5, 463), (733486.61041666672, 1210.5, 1210.75, 1211.25, 1210.0, 483), (733486.61111111112, 1210.75, 1210.75, 1211.5, 1210.0, 887), (733486.61180555553, 1210.75, 1210.25, 1211.0, 1209.75, 556), (733486.61250000005, 1210.0, 1210.75, 1211.25, 1210.0, 615), (733486.61319444445, 1211.0, 1210.75, 1211.5, 1210.5, 545), (733486.61388888885, 1210.5, 1211.75, 1211.75, 1210.5, 707), (733486.61458333337, 1211.75, 1211.5, 1212.5, 1211.0, 857), (733486.61527777778,1211.25, 1213.25, 1213.5, 1211.25, 662), (733486.61597222218, 1212.75, 1212.0, 1213.0, 1211.75, 623), (733486.6166666667, 1212.0,1210.5, 1212.5, 1210.25, 250), (733486.6173611111, 1210.75, 1211.0, 1212.0, 1209.75, 460), (733486.6180555555, 1211.0, 1211.5, 1211.5, 1210.25, 407), (733486.61875000002, 1211.25, 1210.5, 1211.25, 1209.25, 766), (733486.61944444443, 1210.25, 1208.75, 1210.5, 1208.5, 400), (733486.62013888895, 1208.75, 1210.5, 1210.75, 1208.75, 1134), (733486.62083333335, 1210.5, 1211.0, 1211.25, 1209.75, 602), (733486.62152777775, 1210.75, 1212.5, 1212.5, 1210.75, 566), (733486.62222222227, 1212.5, 1211.75, 1212.75, 1211.5, 369), (733486.62291666667, 1211.75, 1213.0, 1213.5, 1211.75, 538), (733486.62361111108, 1213.0, 1213.0, 1213.5, 1212.5, 623), (733486.6243055556, 1213.0, 1213.0, 1214.0, 1212.75, 630), (733486.625, 1213.0, 1212.5, 1213.25, 1212.0, 448), (733486.6256944444, 1212.25, 1212.25, 1212.25, 1211.0, 473), (733486.62638888892, 1212.5, 1210.5, 1212.5, 1210.5, 439), (733486.62708333333, 1210.5, 1211.25, 1211.75, 1210.25, 826), (733486.62777777773, 1211.25, 1211.5, 1211.75, 1210.25, 632), (733486.62847222225, 1211.5, 1209.75, 1211.75, 1209.5, 304), (733486.62916666665, 1209.75, 1207.75, 1209.75, 1207.25, 747), (733486.62986111105, 1207.75, 1208.5, 1208.5, 1207.25, 360), (733486.63055555557, 1208.5, 1208.5, 1209.0, 1208.0, 506), (733486.63124999998, 1208.5, 1209.0, 1209.0, 1207.75, 840), (733486.6319444445, 1209.0, 1208.0, 1209.0, 1207.75, 953), (733486.6326388889, 1208.0, 1207.75, 1208.25, 1206.5, 596), (733486.6333333333, 1207.5, 1206.25, 1207.75, 1206.25, 589), (733486.63402777782, 1206.25, 1206.75, 1207.0, 1206.25, 419), (733486.63472222222, 1206.75, 1207.0, 1207.25, 1206.5, 554), (733486.63541666663, 1207.0, 1206.25, 1207.0, 1205.75, 583), (733486.63611111115, 1206.0, 1204.75, 1206.0, 1204.5, 830), (733486.63680555555, 1204.75, 1206.25, 1206.75, 1204.5, 1211), (733486.63749999995, 1206.5, 1206.25, 1206.75, 1205.75, 594), (733486.63819444447, 1206.25, 1206.0, 1206.5, 1205.5, 431), (733486.63888888888, 1205.75, 1205.75, 1206.25, 1205.25, 393), (733486.63958333328, 1205.75, 1205.25, 1206.0, 1205.0, 294), (733486.6402777778, 1205.25, 1204.0, 1205.5, 1203.75, 726), (733486.6409722222, 1204.0, 1203.5, 1204.75, 1203.25, 953), (733486.64166666672, 1203.5, 1202.25, 1203.5, 1202.25, 696), (733486.64236111112, 1202.25, 1201.25, 1202.5, 1201.0, 935), (733486.64305555553, 1201.0, 1202.0, 1202.25, 1200.75, 815), (733486.64375000005, 1201.75, 1202.25, 1202.75, 1200.75, 1098), (733486.64444444445, 1202.5, 1202.75, 1203.5, 1202.25, 1381), (733486.64513888885, 1202.75, 1202.5, 1203.5, 1202.0, 854), (733486.64583333337, 1202.5, 1203.0, 1203.0, 1202.0, 842), (733486.64652777778,1202.75, 1202.25, 1203.0, 1201.75, 1203), (733486.64722222218, 1202.25, 1203.75, 1204.25, 1202.0, 1036), (733486.6479166667, 1203.75, 1204.5, 1204.75, 1203.75, 652), (733486.6486111111, 1204.5, 1205.0, 1205.5, 1204.5, 636), (733486.6493055555, 1205.0, 1205.0,1205.75, 1204.75, 754), (733486.65000000002, 1204.75, 1205.0, 1205.25, 1204.5, 603), (733486.65069444443, 1204.75, 1205.0, 1205.25, 1204.25, 999), (733486.65138888895, 1205.25, 1204.5, 1205.25, 1204.25, 437), (733486.65208333335, 1204.5, 1204.75, 1205.25, 1204.5, 465), (733486.65277777775, 1204.75, 1204.5, 1205.0, 1204.25, 481), (733486.65347222227, 1204.75, 1205.75, 1206.5, 1204.75, 892), (733486.65416666667, 1205.5, 1206.25, 1206.5, 1205.25, 376), (733486.65486111108, 1206.0, 1205.5, 1206.25, 1205.0, 764), (733486.6555555556, 1205.25, 1205.0, 1205.5, 1204.75, 388), (733486.65625, 1205.0, 1204.75, 1205.0, 1204.25, 279), (733486.6569444444, 1204.75, 1203.5, 1205.0, 1202.75, 1201), (733486.65763888892, 1203.5, 1203.0, 1203.5, 1202.75, 386), (733486.65833333333, 1203.25,1202.75, 1203.5, 1202.25, 731), (733486.65902777773, 1202.75, 1202.75, 1203.25, 1202.25, 683), (733486.65972222225, 1202.75, 1202.25, 1203.0, 1202.25, 265), (733486.66041666665, 1202.5, 1202.75, 1203.0, 1201.75, 604), (733486.66111111105, 1202.5, 1202.5, 1202.5, 1202.0, 541), (733486.66180555557, 1202.5, 1202.5, 1203.25, 1201.75, 923), (733486.66249999998, 1202.5, 1203.25, 1203.75, 1202.25, 421), (733486.6631944445, 1203.25, 1203.75, 1204.0, 1203.25, 308), (733486.6638888889, 1203.75, 1202.5, 1204.0, 1202.0, 611),(733486.6645833333, 1202.25, 1202.0, 1202.75, 1201.5, 327), (733486.66527777782, 1202.25, 1201.75, 1202.25, 1201.75, 209), (733486.66597222222, 1201.75, 1202.25, 1202.5, 1201.75, 332), (733486.66666666663, 1202.25, 1203.0, 1203.0, 1202.25, 246), (733486.66736111115, 1202.5, 1202.5, 1202.75, 1202.25, 400), (733486.66805555555, 1202.25, 1202.75, 1203.25, 1202.25, 313), (733486.66874999995, 1203.0, 1201.0, 1203.0, 1201.0, 533), (733486.66944444447, 1201.0, 1201.75, 1202.0, 1201.0, 231), (733486.67013888888, 1201.75, 1201.25, 1202.0, 1201.0, 285), (733486.67083333328, 1201.0, 1201.5, 1201.75, 1200.5, 343), (733486.6715277778, 1201.5, 1202.0, 1202.0, 1201.5, 217), (733486.6722222222, 1202.0, 1202.25, 1202.5, 1201.75, 354), (733486.67291666672, 1202.0, 1202.0, 1202.25, 1201.75, 74), (733486.67361111112, 1201.75, 1201.5, 1202.5, 1201.25, 454), (733486.67430555553, 1201.75, 1202.5, 1202.75, 1201.25, 337), (733486.67500000005, 1202.5, 1203.25, 1203.25, 1202.25, 709), (733486.67569444445, 1203.25, 1203.5, 1203.75, 1202.75, 537), (733486.67638888885, 1203.25, 1203.5, 1203.5, 1203.0, 191), (733486.67708333337, 1203.25, 1204.0, 1204.25, 1203.25, 555), (733486.67777777778, 1203.75, 1202.25, 1203.75, 1202.25, 270), (733486.67847222218, 1202.0, 1203.0, 1203.0, 1202.0, 481), (733486.6791666667, 1203.0, 1203.25, 1204.0, 1202.75, 855), (733486.6798611111, 1203.25, 1203.5, 1203.75, 1203.25, 220), (733486.6805555555, 1203.75, 1204.75, 1205.25, 1203.5, 892), (733486.68125000002, 1205.0, 1204.0, 1205.25, 1203.5, 577), (733486.68194444443, 1204.0, 1204.75, 1205.0, 1203.75, 1184), (733486.68263888895, 1205.0, 1204.75, 1205.25, 1204.75, 563), (733486.68333333335, 1204.5, 1203.75, 1204.75, 1203.5, 462), (733486.68402777775, 1204.0, 1204.5, 1204.5, 1204.0, 296), (733486.68472222227, 1204.5, 1203.75, 1204.5, 1203.25,482), (733486.68541666667, 1203.5, 1203.5, 1203.75, 1202.5, 417), (733486.68611111108, 1203.75, 1203.5, 1203.75, 1203.0, 255), (733486.6868055556, 1203.75, 1203.0, 1204.0, 1203.0, 358), (733486.6875, 1203.25, 1203.5, 1204.0, 1203.0, 413), (733486.6881944444, 1203.5, 1203.75, 1204.0, 1202.75, 745), (733486.68888888892, 1203.5, 1204.75, 1204.75, 1203.5, 439), (733486.68958333333, 1204.75,1204.25, 1205.0, 1204.25, 328), (733486.69027777773, 1204.25, 1205.0, 1205.5, 1203.75, 1195), (733486.69097222225, 1205.0, 1204.75, 1205.0, 1204.5, 274), (733486.69166666665, 1204.75, 1205.0, 1205.25, 1204.5, 328), (733486.69236111105, 1205.0, 1204.75, 1205.5, 1204.75, 317), (733486.69305555557, 1204.75, 1204.5, 1205.25, 1204.25, 498), (733486.69374999998, 1204.5, 1205.5, 1205.75, 1204.5, 1045), (733486.6944444445, 1205.75, 1203.25, 1205.75, 1202.5, 839), (733486.6951388889, 1203.25, 1203.25, 1204.0, 1202.75, 685), (733486.6958333333, 1203.25, 1202.75, 1203.5, 1201.75, 684), (733486.69652777782, 1202.5, 1201.75, 1203.0, 1201.5, 629), (733486.69722222222, 1201.5, 1201.0, 1201.75, 1201.0, 393), (733486.69791666663, 1201.25, 1202.25, 1202.75, 1201.25, 692), (733486.69861111115, 1202.0, 1202.0, 1202.5, 1201.75, 150), (733486.69930555555, 1202.0, 1201.5, 1202.5, 1201.5, 374), (733486.69999999995, 1201.25, 1199.5, 1201.5, 1199.5, 1269), (733486.70069444447, 1199.75, 1199.75, 1200.5, 1199.5, 572), (733486.70138888888, 1199.75, 1199.5, 1200.0, 1199.25, 329), (733486.70208333328, 1199.5, 1197.75, 1199.5, 1197.5, 1079), (733486.7027777778, 1197.5, 1195.0, 1197.5, 1194.75, 1724), (733486.7034722222, 1194.75, 1194.25, 1195.5, 1193.75, 1710), (733486.70416666672, 1194.25, 1193.75, 1194.5, 1193.0, 2162), (733486.70486111112, 1193.5, 1192.75, 1194.5, 1192.75, 1893), (733486.70555555553, 1192.75, 1191.25, 1193.0, 1190.25,1799), (733486.70625000005, 1191.25, 1192.0, 1192.0, 1191.25, 1016), (733486.70694444445, 1192.0, 1191.75, 1192.5, 1191.25, 1218), (733486.70763888885, 1192.0, 1192.75, 1192.75, 1191.75, 814), (733486.70833333337, 1192.75, 1192.25, 1193.0, 1192.25, 557), (733486.70902777778, 1192.25, 1192.75, 1192.75, 1192.0, 635), (733486.70972222218, 1192.75, 1192.5, 1193.0, 1192.25, 442), (733486.7104166667, 1192.5, 1192.25, 1192.75, 1192.0, 388), (733486.7111111111, 1192.25, 1193.0, 1193.0, 1192.25, 324), (733486.7118055555, 1193.0, 1193.0, 1193.25, 1192.75, 242), (733486.71250000002, 1192.75, 1192.0, 1193.0, 1191.5, 1439), (733486.71319444443, 1192.25, 1192.0, 1192.25, 1191.75, 315), (733486.71388888895, 1192.0, 1192.0, 1192.5, 1191.25, 742), (733486.71458333335, 1191.75, 1192.0, 1192.5, 1191.5, 395), (733486.71527777775, 1192.0, 1191.25, 1192.25, 1191.0, 326), (733486.71597222227, 1191.25, 1191.25, 1191.5, 1190.25, 617), (733486.71666666667, 1191.25, 1190.0, 1191.25, 1189.75, 764), (733486.71736111108, 1190.0, 1190.75, 1191.0, 1190.0,567), (733486.7180555556, 1191.0, 1191.5, 1191.75, 1190.5, 436), (733486.71875, 1191.5, 1192.0, 1192.0, 1191.0, 486), (733486.7194444444, 1192.0, 1191.5, 1192.0, 1191.0, 363), (733486.72013888892, 1191.25, 1191.25, 1191.25, 1190.75, 184), (733486.72083333333,1191.0, 1191.25, 1191.5, 1191.0, 215), (733486.72152777773, 1191.25, 1191.25, 1191.5, 1191.0, 256), (733486.72222222225, 1191.5, 1190.25, 1191.5, 1190.0, 615), (733486.72291666665, 1190.25, 1189.0, 1190.25, 1188.5, 849), (733486.72361111105, 1189.0, 1189.0, 1189.5, 1188.75, 386), (733486.72430555557, 1189.25, 1187.75, 1189.25, 1186.25, 1538), (733486.72499999998, 1187.75, 1186.25, 1187.75, 1186.0, 782), (733486.7256944445, 1186.25, 1187.25, 1187.5, 1186.0, 640), (733486.7263888889, 1187.5, 1186.75, 1187.5, 1186.5,435), (733486.7270833333, 1187.0, 1185.75, 1187.0, 1185.5, 663), (733486.72777777782, 1185.5, 1186.25, 1186.5, 1185.0, 1577), (733486.72847222222, 1186.0, 1185.5, 1186.5, 1185.5, 296), (733486.72916666663, 1185.75, 1185.75, 1186.5, 1185.25, 952), (733486.72986111115, 1185.75, 1186.25, 1186.75, 1185.5, 558), (733486.73055555555, 1186.25, 1186.5, 1186.75, 1186.0, 437), (733486.73124999995, 1186.5, 1188.25, 1188.25, 1186.5, 601), (733486.73194444447, 1188.25, 1188.0, 1188.25, 1187.5, 837), (733486.73263888888, 1187.75, 1188.0, 1188.25, 1186.75, 456), (733486.73333333328, 1188.0, 1186.25, 1188.25, 1186.25, 795), (733486.7340277778, 1186.25, 1186.25, 1186.75, 1185.75, 524), (733486.7347222222, 1186.5, 1185.75, 1186.5, 1185.5, 324), (733486.73541666672, 1185.5, 1186.0, 1186.25, 1185.5, 378), (733486.73611111112, 1186.0, 1184.5, 1186.0, 1183.25, 1522), (733486.73680555553, 1184.25, 1183.0, 1184.5, 1183.0, 828), (733486.73750000005, 1183.25, 1183.75, 1183.75, 1182.75, 706), (733486.73819444445, 1183.75, 1184.0, 1184.5, 1183.75, 873), (733486.73888888885, 1184.0, 1183.5, 1184.25, 1183.25, 465), (733486.73958333337, 1183.25, 1184.0, 1184.5, 1183.25, 441), (733486.74027777778, 1184.0, 1183.75, 1184.5, 1183.25, 863), (733486.74097222218, 1183.75, 1182.75, 1184.25, 1182.75, 608), (733486.7416666667, 1182.75, 1182.25, 1183.0, 1181.75, 1065), (733486.7423611111, 1182.25, 1181.5, 1182.25, 1181.0, 748), (733486.7430555555,1181.5, 1182.25, 1182.25, 1181.25, 525), (733486.74375000002, 1182.25, 1183.0, 1183.5, 1182.0, 465), (733486.74444444443, 1183.25, 1183.25, 1184.0, 1182.5, 848), (733486.74513888895, 1183.0, 1181.75, 1183.25, 1181.5, 1054), (733486.74583333335, 1182.0, 1181.25, 1182.0, 1181.0, 657), (733486.74652777775, 1181.5, 1180.5, 1181.5, 1180.25, 1299), (733486.74722222227, 1180.75, 1180.75, 1181.0, 1180.25, 517), (733486.74791666667, 1180.75, 1181.5, 1182.0, 1180.5, 941), (733486.74861111108, 1181.5, 1181.0, 1182.0, 1181.0,701), (733486.7493055556, 1181.25, 1180.0, 1181.5, 1180.0, 762), (733486.75, 1180.25, 1180.75, 1181.25, 1180.25, 579), (733486.7506944444, 1180.75, 1180.0, 1181.5, 1179.25, 1415), (733486.75138888892, 1180.0, 1180.5, 1180.75, 1179.5, 844), (733486.75208333333, 1180.5, 1180.0, 1180.75, 1180.0, 381), (733486.75277777773, 1180.25, 1180.75, 1180.75, 1180.0, 586), (733486.75347222225, 1180.75, 1181.25, 1181.25, 1179.5, 740), (733486.75416666665, 1181.25, 1179.0, 1181.25, 1178.0, 1625), (733486.75486111105, 1179.0, 1179.5, 1179.5, 1178.5, 336), (733486.75555555557, 1179.5, 1179.5, 1180.25, 1178.75, 803), (733486.75624999998, 1179.75, 1178.5, 1179.75, 1178.5, 614), (733486.7569444445, 1178.75, 1178.0, 1179.0, 1177.25, 1512), (733486.7576388889, 1178.0, 1179.5, 1179.5, 1177.5,1106), (733486.7583333333, 1179.5, 1179.5, 1180.0, 1178.75, 998), (733486.75902777782, 1179.5, 1179.0, 1180.25, 1178.5, 735), (733486.75972222222, 1179.25, 1179.75, 1180.0, 1178.75, 490), (733486.76041666663, 1179.75, 1180.75, 1181.0, 1179.5, 601), (733486.76111111115, 1181.0, 1181.75, 1182.0, 1180.25, 793), (733486.76180555555, 1181.75, 1181.25, 1181.75, 1180.75, 479), (733486.76249999995, 1181.0, 1181.5, 1182.5, 1181.0, 419), (733486.76319444447, 1181.75, 1181.5, 1182.25, 1181.0, 431), (733486.76388888888, 1181.5, 1181.5, 1182.0, 1181.25, 333), (733486.76458333328, 1181.5, 1182.25, 1182.5, 1181.25, 402), (733486.7652777778, 1182.25, 1182.25, 1183.0, 1181.5, 421), (733486.7659722222, 1182.0, 1181.5, 1183.0, 1181.5, 406), (733486.76666666672, 1181.5, 1182.5, 1182.5, 1181.0, 766), (733486.76736111112, 1182.5, 1181.75, 1182.5, 1181.25, 387), (733486.76805555553, 1181.75, 1180.75, 1182.0, 1180.75, 523), (733486.76875000005, 1180.75, 1180.75, 1181.25, 1180.0, 590), (733486.76944444445, 1180.75, 1182.0, 1182.25, 1180.75, 422), (733486.77013888885, 1182.25, 1183.0, 1183.25, 1182.0, 418), (733486.77083333337, 1183.25, 1184.0, 1184.5, 1182.75, 554), (733486.77152777778, 1184.0, 1184.5, 1185.25, 1183.75, 1071), (733486.77222222218, 1184.5, 1185.0, 1185.25, 1183.75, 582), (733486.7729166667, 1185.0, 1184.5, 1185.5, 1184.25, 542), (733486.7736111111, 1184.25, 1184.25, 1184.75, 1183.75, 397), (733486.7743055555, 1184.25, 1184.0, 1185.25, 1183.75, 545), (733486.77500000002, 1184.0, 1183.25, 1184.0, 1182.75, 669), (733486.77569444443, 1183.25, 1183.5, 1184.25, 1183.0, 466), (733486.77638888895, 1183.75, 1184.0, 1184.75, 1183.0, 347), (733486.77708333335, 1184.0, 1184.0, 1184.25, 1183.75, 240), (733486.77777777775, 1184.25, 1185.0, 1185.5, 1184.0, 667), (733486.77847222227, 1185.25, 1184.25, 1185.5, 1184.0, 559), (733486.77916666667, 1184.5, 1183.25, 1184.5, 1183.0, 720), (733486.77986111108, 1183.0, 1182.75, 1184.75, 1182.75, 1140), (733486.7805555556, 1182.75, 1180.75, 1182.75, 1179.75, 1758), (733486.78125, 1180.75, 1181.0, 1181.25, 1179.5, 1052), (733486.7819444444, 1181.25, 1180.75, 1181.25, 1180.0, 934), (733486.78263888892, 1180.75, 1181.25, 1182.0, 1180.75, 735), (733486.78333333333, 1181.25, 1180.75, 1181.5, 1180.5, 296), (733486.78402777773, 1180.75, 1182.0, 1182.25, 1180.5, 357), (733486.78472222225, 1182.0, 1181.75, 1182.5, 1181.25, 353), (733486.78541666665, 1181.5, 1181.5, 1181.75, 1180.75, 592), (733486.78611111105, 1181.5, 1181.5, 1181.75, 1180.75, 339), (733486.78680555557, 1181.5, 1180.25, 1181.5, 1180.25, 248), (733486.78749999998, 1180.5, 1181.0, 1181.25, 1180.5, 258), (733486.7881944445, 1181.25, 1181.25, 1181.75, 1181.0, 334), (733486.7888888889, 1181.25, 1181.25, 1181.75, 1180.0, 714), (733486.7895833333, 1181.0, 1182.25, 1182.5, 1180.5, 469), (733486.79027777782, 1182.25, 1184.5, 1184.75, 1182.0, 1000), (733486.79097222222, 1184.5, 1185.5, 1186.25, 1184.0, 1072), (733486.79166666663, 1185.25, 1185.0, 1186.0, 1184.75, 481), (733486.79236111115, 1185.25, 1187.0, 1187.0, 1185.0, 719), (733486.79305555555, 1187.0, 1187.5, 1187.75, 1186.75, 1308), (733486.79374999995, 1187.5, 1187.25, 1188.0, 1187.0, 1096), (733486.79444444447, 1187.25, 1186.75, 1187.75, 1186.5, 861), (733486.79513888888, 1186.75, 1186.0, 1187.25, 1185.75, 816), (733486.79583333328, 1186.0, 1186.75, 1187.0, 1185.5, 818), (733486.7965277778, 1186.5, 1186.5, 1187.25, 1186.0, 321), (733486.7972222222, 1186.25, 1186.0, 1187.75, 1186.0, 476), (733486.79791666672, 1186.0, 1187.0, 1187.25, 1185.75, 475), (733486.79861111112, 1187.25, 1187.25, 1188.0, 1186.75, 376), (733486.79930555553, 1187.0, 1189.25, 1189.75, 1187.0, 1344), (733486.80000000005, 1189.25, 1189.25, 1189.5, 1188.25, 541), (733486.80069444445, 1189.0, 1186.25, 1189.0, 1186.25, 1078), (733486.80138888885, 1186.5, 1185.75, 1186.75, 1185.25, 679), (733486.80208333337, 1186.0, 1184.75, 1186.5, 1184.75, 481), (733486.80277777778, 1184.75, 1184.25, 1185.5, 1184.0, 1325), (733486.80347222218, 1184.25, 1184.0, 1184.5, 1183.75, 557), (733486.8041666667, 1184.0, 1184.5, 1184.5, 1184.0, 442), (733486.8048611111, 1184.5, 1184.5, 1184.5, 1184.0, 330), (733486.8055555555, 1184.5, 1184.0, 1185.0, 1183.5, 871), (733486.80625000002, 1183.75, 1184.25, 1185.0, 1183.75, 324), (733486.80694444443, 1184.5, 1185.0, 1185.25, 1184.25, 384), (733486.80763888895, 1184.75, 1186.25, 1186.75, 1184.75, 1602), (733486.80833333335, 1186.25, 1187.0, 1187.5, 1186.0, 503), (733486.80902777775, 1187.0, 1188.75, 1189.0, 1186.75, 606), (733486.80972222227, 1188.5, 1188.0, 1188.75, 1188.0, 682), (733486.81041666667, 1188.0, 1188.25, 1188.75, 1187.75, 625), (733486.81111111108, 1188.25, 1188.75, 1189.5, 1187.75, 755), (733486.8118055556, 1188.5, 1188.25, 1188.75, 1188.0, 368), (733486.8125, 1188.5, 1187.5, 1188.75, 1186.75, 666), (733486.8131944444, 1187.75, 1189.25, 1189.75, 1187.5, 588), (733486.81388888892, 1189.5, 1188.0, 1189.75, 1188.0, 425), (733486.81458333333, 1188.25, 1188.0, 1188.25, 1187.5, 442), (733486.81527777773, 1187.75, 1190.75, 1190.75, 1187.5, 1833), (733486.81597222225, 1190.5, 1192.5, 1192.75, 1190.5, 2101), (733486.81666666665, 1192.5, 1191.5, 1193.0, 1191.25, 1333), (733486.81736111105, 1191.25, 1193.5, 1194.25, 1191.25, 1367), (733486.81805555557, 1193.5, 1192.25, 1194.0, 1192.0, 778), (733486.81874999998, 1192.25, 1192.0, 1193.5, 1191.5, 678), (733486.8194444445, 1191.75, 1190.25, 1191.75, 1189.0, 1230), (733486.8201388889, 1190.25, 1189.25, 1191.25, 1188.75, 1085), (733486.8208333333, 1189.25, 1189.75, 1189.75, 1188.0, 743), (733486.82152777782, 1189.5, 1188.75, 1190.25,1187.75, 881), (733486.82222222222, 1189.25, 1188.5, 1190.25, 1187.75, 941), (733486.82291666663, 1188.5, 1188.25, 1189.75, 1188.0, 584), (733486.82361111115, 1188.5, 1188.25, 1189.0, 1188.0, 493), (733486.82430555555, 1188.25, 1187.0, 1188.5, 1186.25, 635), (733486.82499999995, 1187.25, 1187.0, 1188.5, 1186.75, 592), (733486.82569444447, 1187.25, 1186.75, 1187.5, 1186.0, 767), (733486.82638888888, 1186.75, 1185.75, 1187.0, 1185.25, 636), (733486.82708333328, 1186.0, 1186.75, 1187.5, 1186.0, 618), (733486.8277777778, 1186.75, 1187.0, 1188.25, 1186.75, 391), (733486.8284722222, 1187.25, 1188.5, 1188.75, 1186.75, 399), (733486.82916666672, 1188.75, 1188.0, 1188.75, 1187.5, 344), (733486.82986111112, 1188.0, 1188.0, 1188.25, 1186.75, 768), (733486.83055555553, 1188.25, 1186.75, 1188.25, 1186.0, 2213), (733486.83125000005, 1186.75, 1187.0, 1187.75, 1186.75, 479), (733486.83194444445, 1187.25, 1186.5,1187.5, 1186.25, 987), (733486.83263888885, 1186.5, 1185.5, 1186.75, 1185.0, 1619), (733486.83333333337, 1185.5, 1185.25, 1186.25, 1184.75, 2995), (733486.83402777778, 1185.25, 1187.75, 1187.75, 1185.0, 1798), (733486.83472222218, 1187.75, 1187.0, 1188.0, 1186.5, 955), (733486.8354166667, 1187.0, 1187.25, 1187.75, 1186.75, 1007), (733486.8361111111, 1187.0, 1186.5, 1187.0, 1186.25, 745), (733486.8368055555, 1186.75, 1187.5, 1187.5, 1186.75, 979), (733486.83750000002, 1187.5, 1187.5, 1188.0, 1187.0, 680), (733486.83819444443, 1187.5, 1187.25, 1187.75, 1187.0, 272), (733486.83888888895, 1187.25, 1187.25, 1187.5, 1187.0, 181), (733486.83958333335, 1187.25, 1187.5, 1187.5, 1186.5, 348), (733486.84027777775, 1187.5, 1187.5, 1188.0, 1187.25, 280), (733486.84097222227, 1187.5, 1187.75, 1188.25, 1187.5, 370), (733486.84166666667, 1188.0, 1186.5, 1188.25, 1186.5, 590), (733486.84236111108, 1186.5, 1187.0,1187.25, 1186.25, 421), (733486.8430555556, 1187.0, 1187.5, 1187.5, 1186.75, 482), (733486.84375, 1187.25, 1187.0, 1187.5, 1186.75, 676)]

def candles(ax, quotes, width=0.2, colorup='k', colordown='r', alpha=1.0):

    OFFSET = width/2.0

    lines = []
    patches = []
    for q in quotes:
        t, open, close, high, low = q[:5]

        if close>=open :
            color = colorup
            lower = open
            height = close-open
            lineColor = colorup
        else  :
            color = colordown
            lower = close
            height = open-close
            lineColor = colordown

        vline = Line2D(
            xdata=(t, t), ydata=(low, high),
            #color='k',
            color = lineColor,
            linewidth=0.5,
	    ls = "-",
            antialiased=True,
            animated=True,
            )
	vline.set_animated(True)
        rect = Rectangle(
            xy    = (t-OFFSET, lower),
            width = width,
            height = height,
            facecolor = color,
            edgecolor = color,
            picker = 5
        #    animated=True,
            )
	rect.set_animated(True)
        #rect.set_alpha(alpha)


        lines.append(vline)
        patches.append(rect)
        ax.add_line(vline)
        ax.add_patch(rect)
    #ax.autoscale_view()

    return lines, patches

class UpdateLine(object):
    def get_bg_bbox(self):
        
        return self.ax.bbox.padded(-3)
    
    def __init__(self, canvas, ax):
        self.cnt = 0
	self.barsToDisplay = 100
        self.canvas = canvas
        self.ax = ax

        self.prev_time = time.time()
        self.start_time = self.prev_time
        
	#set x0 to the first bar
        self.x0 = dummyData[0][0]
	#the dummy data have a Y range of about 1175 to 1220
        self.ax.set_ylim(1175, 1220)

        self.background1 = None

	#the candlesticks are pre-generated so we only have to plot them when the graph is moving.
        self.lines, self.patches = candles( self.ax, dummyData, width = 0.0003, colorup = 'g' )

	self.locator = mdates.MinuteLocator(range(0,60, 15))

	self.noOfBarsToDisplay = len(dummyData) - 1
	


    def save_bg(self):
        self.background1 = self.canvas.copy_from_bbox(self.ax.get_figure().bbox)

        self.background2 = self.canvas.copy_from_bbox(self.get_bg_bbox())


    def get_dx_data(self, dx_pixel):
        tp = self.ax.transData.inverted().transform_point
        x0, y0 = tp((0, 0))
        x1, y1 = tp((dx_pixel, 0))
        return (x1-x0)

    def get_dx_pixel(self, dx_data):
        tp = self.ax.transData.transform_point
        x0, y0 = tp((0, 0))
        x1, y1 = tp((dx_data, 0))
        return (x1-x0)



    def restore_background_shifted(self, dx_pixel):
        """
        restore bacground shifted by dx in data coordinate. This only
        works if the data coordinate system is linear.
        """

        # restore the clean slate background
        self.canvas.restore_region(self.background1)

        # restore subregion (x1+dx, y1, x2, y2) of the second bg 
        # in a offset position (x1-dx, y1)
        x1, y1, x2, y2 = self.background2.get_extents()
        self.canvas.restore_region(self.background2,
                                   bbox=(x1+dx_pixel, y1, x2, y2),
                                   xy=(x1-dx_pixel, y1))

        return dx_pixel

    def on_draw(self, *args):
        self.save_bg()
        return False
    
    def update_line(self, *args):

        if self.background1 is None:
            return True
        
        cur_time = time.time()
	dx_data = 1.0/24/60	#0.00069444 #1minute

	dx_pixel = self.get_dx_pixel( dx_data)
        
        self.x0 += dx_data
        self.prev_time = cur_time

        self.ax.set_xlim(self.x0-2.0/24, self.x0+dx_data)
	self.ax.xaxis_date()
	ax.xaxis.set_major_locator(self.locator)
	ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
	

        # restore background which will plot lines from previous plots
        self.restore_background_shifted(dx_pixel) #x0, self.x0)
        # This restores lines between [x0-2, x0]


        # now plot line segment within [x0, x0+dx_data], 
        # Note that we're only plotting a line between [x0, x0+dx_data].

	self.ax.draw_artist(self.lines[self.cnt])
	self.ax.draw_artist(self.patches[self.cnt])

        self.background2 = canvas.copy_from_bbox(self.get_bg_bbox())


        self.ax.draw_artist(self.ax.xaxis)
        self.ax.draw_artist(self.ax.yaxis)

        self.canvas.blit(self.ax.get_figure().bbox)


	time.sleep(0.1)
        dt = (time.time()-tstart)
        #if dt>5:
	if self.cnt >= self.noOfBarsToDisplay:
            # print the timing info and quit
            print 'FPS:' , self.cnt/dt
            gtk.main_quit()
            raise SystemExit

        self.cnt += 1
        return True


plt.rcParams["text.usetex"] = False
fig = plt.figure()

ax = fig.add_subplot(111)
ax.xaxis.set_animated(True)
ax.yaxis.set_animated(True)

canvas = fig.canvas

fig.subplots_adjust(left=0.1, bottom=0.2)

canvas.draw()

# for profiling
tstart = time.time()

ul = UpdateLine(canvas, ax)
gobject.idle_add(ul.update_line)

canvas.mpl_connect('draw_event', ul.on_draw)

plt.show()
