import matplotlib.pyplot as plt

# Measurements from the table
expt = [
    (4,1,[
        (128, [[5.940000057, 293, 1396480, 0.647645058, 0.23413336, 3.25],[7.2099998, 306, 1541178, 0.806241821, 0.750284879, 5.149999857],[5.620000124, 310, 1556392, 0.752677412, 0.294059595, 2.670000076],[10.33999991, 425, 1567290, 0.767552938, 0.722960109, 4.75999999]]),
        (256, [[19.1500001, 276, 2833410, 1.421521749, 5.718475913, 16.06999993],[21.70000005, 263, 2928778, 1.825893544, 11.81559673, 18.67000008],[11.3499999, 224, 2447490, 0.940714284, 1.58701201, 6.690000057],[11.68000007, 243, 2639845, 1.010452671, 1.774796935, 10.30999994],[7.899999857, 296, 3090702, 1.117500002, 1.337155898, 6.069999933],[10.36999989, 314, 3093448, 1.181082799, 2.096159356, 7.279999971]]),
        (512, [[19.06999993, 226, 5712312, 2.412964605, 13.40428096, 17.77999997],[87.38000011, 482, 6184016, 5.820580907, 295.0460774, 82.98999977],[29.67000008, 294, 5714713, 1.941870746, 13.56463148, 28.17999983],[40.53999996, 252, 5584736, 2.493888873, 38.60236743, 34.68000007],[36.72000003, 268, 5649080, 2.364328353, 28.42150058, 31.20000005]]),
        (1024, [[27.11999989, 258, 11793487, 2.097054248, 15.23283466, 20.56999993],[29.98000002, 282, 12308096, 3.841666675, 39.74736357, 25.87999988],[16.97000003, 248, 10128923, 1.322782259, 3.864306318, 13.64999986],[24.73000002, 249, 11409228, 2.415060244, 15.76277366, 20.0999999],[33.74000001, 297, 11666541, 2.900168342, 27.40797012, 25.61000013]]),
        (2048, [[54.5, 282, 21010283, 4.64677305, 73.69933616, 37.94999981],[73.30999994, 242, 23568893, 9.918966927, 239.5914324, 63.45000005],[70.8499999, 227, 23312093, 9.411982361, 231.4646233, 64.12000012]])
    ], '--o'),
    (8,2,[
        (128, [[5.230000019, 2255, 6157583, 0.682984484, 0.22252799, 4.069999933],[5.99000001, 2437, 6134364, 0.701973739, 0.331131886, 5.059999943],[5.450000048, 2371, 6093145, 0.706111342, 0.309728953, 3.910000086],[6.150000095, 2660, 6211287, 0.726812037, 0.394393744, 4.810000181],[6.859999895, 2900, 6281059, 0.716162067, 0.339368136, 5.330000162]]),
        (256, [[12.31999993, 3102, 12437395, 1.123684721, 2.238323991, 9.789999962],[9.649999857, 2487, 12196387, 1.074238039, 1.507334197, 8.650000095],[12.06999993, 2767, 12261469, 1.104803034, 2.011254519, 10.18000007],[9.109999895, 2484, 12253370, 0.981489531, 1.284007355, 7]]),
        (512, [[17.39999986, 2381, 23719388, 1.799202015, 7.454828266, 14.56999993],[20.87000012, 3738, 24853988, 1.500125735, 6.202361069, 15.48000002],[19.25, 2924, 24739350, 1.750058136, 8.150292917, 15.70000005]]),
        (1024, [[38.37999988, 3523, 49092077, 2.734007947, 25.16038619, 29.52000022],[38.36999989, 3286, 47280429, 2.590833839, 23.67362793, 32.25999999],[36.27999997, 2895, 48386472, 3.051243521, 29.00638734, 31.6099999]]),
        (8192, [[55.75999999, 5313, 95182622, 2.727918312, 43.78314273, 45.75999999],[52.33999991, 4985, 94242230, 2.402098303, 33.93348783, 46.01999998],[319.03, 4915, 94097996, 12.152765, 2208.017538, 316.1799998],[48.97000003, 5606, 98027752, 2.663995716, 34.43750126, 43.26999998]])
    ], '--^'),
    (16, 4,[
        (128, [[26.99000001, 56932, 31694811, 0.761393947, 0.879322338, 24.30999994]]),
        (256, [[46.6500001, 57836, 59040154, 1.166456537, 2.762598404, 18.12999988]]),
        (512, [[59.63000011, 57301, 111335920, 1.438934224, 6.138471836, 48.33000016]]),
    ], '--*')
    ]



def do_plot():
    f = plt.figure(1, figsize=(7,5));
    plt.clf()
    ax = f.add_subplot(1, 1, 1)
    for N,t, entries, style in expt:
        latency = []
        latencyErrorL = []
        latencyErrorU = []
        maxLat = []
        msgDelay = []
        variance = []
        batch = []
        for ToverN, samples in entries:
            batch.append(N*ToverN)
            latencyMean = sum([s[0] for s in samples]) / float(len(samples))
            latency.append(latencyMean)
            latencyErrorL.append(latencyMean - min([s[0] for s in samples]))
            latencyErrorU.append(max([s[0] for s in samples]) - latencyMean)
        # ax.plot(batch, latency, style, label='%d/%d' % (N,t))
        ax.errorbar(batch, latency, yerr=[latencyErrorL, latencyErrorU], label='%d/%d' % (N,t), fmt=style)
    plt.legend(title='Nodes / Tolerance', loc='best')
    plt.ylabel('Latency (sec)')
    ax.set_xscale("log")
    ax.set_yscale("log")
    #plt.ylabel('Latency')
    #plt.xlabel('Throughput')
    plt.xlabel('Requests (Tx)')
    # plt.tight_layout()
    # plt.show()
    plt.savefig('plot_latency_tor.pdf', format='pdf', dpi=1000)

do_plot()
