File: results_mpl.py

package info (click to toggle)
sumo 1.18.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,218,476 kB
  • sloc: xml: 2,488,246; cpp: 431,611; python: 236,255; java: 14,424; cs: 5,200; ansic: 494; sh: 474; makefile: 80; csh: 1
file content (3445 lines) | stat: -rw-r--r-- 183,592 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2016-2023 German Aerospace Center (DLR) and others.
# SUMOPy module
# Copyright (C) 2012-2021 University of Bologna - DICAM
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later

# @file    results_mpl.py
# @author  Joerg Schweizer
# @date   2012

from numpy.linalg import inv
import os
##import math
import numpy as np
from collections import OrderedDict
import matplotlib.pyplot as plt
from agilepy.lib_base.geometry import *
from matplotlib.path import Path
import matplotlib.patches as patche
from coremodules.demand.origin_to_destination import OdIntervals
from agilepy.lib_base.misc import get_inversemap
from coremodules.misc.matplottools import *
from coremodules.network import network
import agilepy.lib_base.classman as cm
import agilepy.lib_base.arrayman as am
from agilepy.lib_base.geometry import *
from agilepy.lib_base.processes import Process
from mapmatching import COLOR_MATCHED_ROUTE, COLOR_SHORTEST_ROUTE, COLOR_FASTEST_ROUTE
import time
try:
    from scipy import interpolate
    is_scipy = True
except:
    is_scipy = False


def is_sublist(l, s):
    sub_set = False
    if s == []:
        sub_set = True
    elif s == l:
        sub_set = True
    elif len(s) > len(l):
        sub_set = False

    else:
        for i in range(len(l)):
            if l[i] == s[0]:
                n = 1
                while (n < len(s)) and (l[i+n] == s[n]):
                    n += 1

                if n == len(s):
                    sub_set = True

    return sub_set


def kf_update(X, P, Y, H, R):
    IM = dot(H, X)
    IS = R + dot(H, dot(P, H.T))
    K = dot(P, dot(H.T, inv(IS)))
    X = X + dot(K, (Y-IM))
    P = P - dot(K, dot(IS, K.T))
    LH = gauss_pdf(Y, IM, IS)
    return (X, P, K, IM, IS, LH)


def gauss_pdf(X, M, S):
    if M.shape()[1] == 1:
        DX = X - tile(M, X.shape()[1])
        E = 0.5 * sum(DX * (dot(inv(S), DX)), axis=0)
        E = E + 0.5 * M.shape()[0] * log(2 * pi) + 0.5 * log(det(S))
        P = exp(-E)
    elif X.shape()[1] == 1:
        DX = tile(X, M.shape()[1]) - M
        E = 0.5 * sum(DX * (dot(inv(S), DX)), axis=0)
        E = E + 0.5 * M.shape()[0] * log(2 * pi) + 0.5 * log(det(S))
        P = exp(-E)
    else:
        DX = X-M
        E = 0.5 * dot(DX.T, dot(inv(S), DX))
        E = E + 0.5 * M.shape()[0] * log(2 * pi) + 0.5 * log(det(S))
        P = exp(-E)
    return (P[0], E[0])


SELECTPOINTS = {'FromOrigin': 1,
                'ToDestination': 2,
                'FromOriginToDestination': 3,
                'All': 4,
                }
PLOTTYPE = {'Isochrone': 1,
            'Heatmap': 2,
            }
POINTSTYPE = {'OnlyOriginPoints': 1,
              'OnlyDestinationPoints': 2,
              'OnlyOriginAndDestinationPoints': 3,
              'All': 4,
              }


class PointresultPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Plot point results with Matplotlib',
                 info="Creates plots of different point results using matplotlib",
                 logger=None, **kwargs):

        self._init_common('pointresultplotter', parent=results, name=name,
                          info=info, logger=logger)

        # print 'Resultplotter.__init__',results,self.parent
        attrsman = self.get_attrsman()
        mapmatching = self.parent.parent
        scenario = mapmatching.get_scenario()
        self.zones = scenario.landuse.zones

        self.select_points = attrsman.add(cm.AttrConf('select_points', kwargs.get('select_points', SELECTPOINTS['FromOrigin']),
                                                      groupnames=['options'],
                                                      choices=SELECTPOINTS,
                                                      name='Select points',
                                                      info='Select trip as indicated',
                                                      ))
        self.plot_type = attrsman.add(cm.AttrConf('plot_type', kwargs.get('plot_type', PLOTTYPE['Heatmap']),
                                                  groupnames=['options'],
                                                  choices=PLOTTYPE,
                                                  name='Plot type',
                                                  info='Choice the type of plot',
                                                  ))
        self.points_type = attrsman.add(cm.AttrConf('points_type', kwargs.get('points_type', POINTSTYPE['All']),
                                                    groupnames=['options'],
                                                    choices=POINTSTYPE,
                                                    name='Points type',
                                                    info='Plot only trip points of the indicated type',
                                                    ))

# self.is_plot_isochrone_generated = attrsman.add(cm.AttrConf(  'is_plot_isochrone_generated', kwargs.get('is_plot_isochrone_generated', True),
##                                        groupnames = ['options'],
##                                        name = 'Plot isochrone map from the origin zone',
##                                        info = 'Plot isochrone map on edge-network for trips starting from the origin zone.',
# ))
# self.is_plot_isochrone_attracted = attrsman.add(cm.AttrConf(  'is_plot_isochrone_attracted', kwargs.get('is_plot_isochrone_attracted', True),
##                                        groupnames = ['options'],
##                                        name = 'Plot isochrone map to the destination zone',
##                                        info = 'Plot isochrone map on edge-network for trips ending to the origin zone.',
# ))
        self.is_isochrone = attrsman.add(cm.AttrConf('is_isochrone', kwargs.get('is_isochrone', False),
                                                     groupnames=['options'],
                                                     name='Plot isochrones in the map',
                                                     info='Plot isochrones lines in the map.',
                                                     ))
        self.max_time = attrsman.add(cm.AttrConf('max_time', kwargs.get('max_time', 40),
                                                 groupnames=['options'],
                                                 perm='wr',
                                                 name='Max time',
                                                 info='Max time to be considered for the isochrone plot.',
                                                 ))
        self.min_n_values = attrsman.add(cm.AttrConf('min_n_values', kwargs.get('min_n_values', 2),
                                                     groupnames=['options'],
                                                     perm='wr',
                                                     name='Minumun number of values',
                                                     info='Minumun number of values to assign a time to each edge.',
                                                     ))
# self.is_plot_points_gen_heatmap = attrsman.add(cm.AttrConf(  'is_plot_points_gen_heatmap', kwargs.get('is_plot_points_gen_heatmap', False),
##                                        groupnames = ['options'],
##                                        name = 'Plot generated trip points from the origin zone',
##                                        info = 'Plot a density heatmap of generated trip points from a zone.',
# ))
# self.is_plot_points_attr_heatmap = attrsman.add(cm.AttrConf(  'is_plot_points_attr_heatmap', kwargs.get('is_plot_points_attr_heatmap', False),
##                                        groupnames = ['options'],
##                                        name = 'Plot attracted trip points from the destination zone',
##                                        info = 'Plot a density heatmap of attracted trip points from a zone.',
# ))
# self.is_plot_points_od_heatmap = attrsman.add(cm.AttrConf(  'is_plot_points_od_heatmap', kwargs.get('is_plot_points_od_heatmap', False),
##                                        groupnames = ['options'],
##                                        name = 'Plot od trip points  heatmap',
##                                        info = 'Plot a density heatmap of trip points from the origin zone to the destination zone.',
# ))
# self.is_plot_points_heatmap = attrsman.add(cm.AttrConf(  'is_plot_points_heatmap', kwargs.get('is_plot_points_heatmap', False),
##                                        groupnames = ['options'],
##                                        name = 'Plot density points heatmap',
##                                        info = 'Plot density heatmap of the selected GPS points.',
# ))

        self.origin_zone_name = attrsman.add(cm.AttrConf('origin_zone_name', kwargs.get('origin_zone_name', self.zones.ids_sumo[0]),
                                                         groupnames=['options'],
                                                         choices=self.zones.ids_sumo,
                                                         name='Origin zone name',
                                                         info='Generating zone name.',
                                                         ))

        self.dest_zone_name = attrsman.add(cm.AttrConf('dest_zone_name', kwargs.get('dest_zone_name', self.zones.ids_sumo[0]),
                                                       groupnames=['options'],
                                                       choices=self.zones.ids_sumo,
                                                       name='Destination zone name',
                                                       info='Attracting zone name.',
                                                       ))

        self.bins = attrsman.add(cm.AttrConf('bins', kwargs.get('bins', 500),
                                             groupnames=['options'],
                                             perm='wr',
                                             name='Bins',
                                             info='Bins for the heatmap of points.',
                                             ))

        self.vmin = attrsman.add(cm.AttrConf('vmin', kwargs.get('vmin', 0),
                                             groupnames=['options'],
                                             perm='wr',
                                             name='Min value',
                                             info='Min value of points in a bin for the heatmap of points.',
                                             ))

        self.is_net = attrsman.add(cm.AttrConf('is_net', kwargs.get('is_net', False),
                                               groupnames=['options'],
                                               name='Show net',
                                               info='Show the network in the heatmap of points.',
                                               ))

        self.titletext = attrsman.add(cm.AttrConf('titletext', kwargs.get('titletext', ''),
                                                  groupnames=['options'],
                                                  name='Title text',
                                                  info='Title text. Empty text means no title.',
                                                  ))
        self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont', 32),
                                                       groupnames=['options'],
                                                       name='Title fontsize',
                                                       info='Title fontsize.',
                                                       ))

        self.add_plotoptions(**kwargs)

        self.add_save_options(**kwargs)

    def show(self):
        # init
        self.init_figures()
        fig = self.create_figure()
        results = self.parent
        ax = fig.add_subplot(111)
        mapmatching = self.parent.parent
        scenario = mapmatching.get_scenario()
        points = scenario.demand.mapmatching.points
        trips = scenario.demand.mapmatching.trips
        zones = scenario.landuse.zones
        net = scenario.net
        ids_edge = net.edges.get_ids()
        ids_trip = trips.get_ids()
        zone_shape_origin = zones.shapes[zones.ids_sumo.get_id_from_index(self.origin_zone_name)]
        zone_shape_dest = zones.shapes[zones.ids_sumo.get_id_from_index(self.dest_zone_name)]

        # Plot net
        if self.is_net:
            print('plot net')
            plot_net(ax, net, color_edge="gray", width_edge=2, color_node=None,
                     alpha=0.5)
        # Plot zones
        if self.select_points == 1 or self.select_points == 3:
            # plot origin zone
            plot_zone(ax, zone_shape_origin, color_zone="coral",
                      color_outline='black', width_line=2,
                      alpha=0.4, zorder=+201)
        if self.select_points == 2 or self.select_points == 3:
            # plot destination zone
            plot_zone(ax, zone_shape_dest, color_zone="coral",
                      color_outline='black', width_line=2,
                      alpha=0.4, zorder=+201)
        # deselect points
        points.are_selected[points.get_ids()] = False
        # Take into account only selected traces with at least two point
        ids_trip = ids_trip[(trips.are_selected[ids_trip] == True) & (trips.ids_points[ids_trip] != int)]
        ids_points = trips.ids_points[ids_trip]

        # select points
        ids_final_point = np.zeros((len(ids_trip)), dtype=np.int32)
        ids_initial_point = np.zeros((len(ids_trip)), dtype=np.int32)
        for id_trip, ids_point, i in zip(trips.get_ids(), ids_points, range(len(ids_trip))):
            ids_final_point[i] = ids_point[-1]
            ids_initial_point[i] = ids_point[0]
            if self.select_points == 1:
                points.are_selected[ids_point] = is_point_in_polygon(
                    points.coords[ids_initial_point[i]], zone_shape_origin)
            if self.select_points == 2:
                points.are_selected[ids_point] = is_point_in_polygon(points.coords[ids_final_point[i]], zone_shape_dest)
            if self.select_points == 3:
                points.are_selected[ids_point] = is_point_in_polygon(
                    points.coords[ids_initial_point[i]], zone_shape_origin)*is_point_in_polygon(points.coords[ids_final_point[i]], zone_shape_dest)
            if self.select_points == 4:
                points.are_selected[ids_point] = True

        # consider only selected points
        if self.points_type == 1:
            ids_point = ids_initial_point[(points.are_selected[ids_initial_point] == True)]
        if self.points_type == 2:
            ids_point = ids_final_point[(points.are_selected[ids_final_point] == True)]
        if self.points_type == 3:
            ids_initial_point = ids_initial_point[(points.are_selected[ids_initial_point] == True)]
            ids_initial_point.tolist()
            ids_final_point = ids_final_point[(points.are_selected[ids_final_point] == True)]
# ids_final_point.tolist()
            ids_point = ids_initial_point.tolist()
            ids_point.extend(ids_final_point)
            print(ids_initial_point, ids_final_point, ids_points)
        if self.points_type == 4:
            ids_point = points.get_ids(points.are_selected.get_value() == True)
        coords = points.coords[ids_point]

        if self.plot_type == 1:
            # Match time points with edges and call isochrone plot, if selected
            min_n_values = self.min_n_values
            times_edge = np.zeros(np.max(ids_edge))
            values = np.zeros(np.max(ids_edge))
            valuess = []
            for i in range(np.max(ids_edge)-1):
                valuess.append([])
            count = np.zeros(np.max(ids_edge))
# if self.is_isochrone:
##                times_point = np.zeros(np.max(ids_point)+1)
            for coord, id_point in zip(coords, ids_point):
                coord = np.array([coord[0], coord[1]])
                id_closest_edge = net.edges.get_closest_edge(coord)
                print('point', id_point)
                if id_closest_edge > 0:
                    if self.select_points == 1:
                        time = (points.timestamps[id_point] - trips.timestamps[points.ids_trip[id_point]])/60.0
                    if self.select_points == 2:
                        time = -(points.timestamps[id_point] -
                                 points.timestamps[trips.ids_points[points.ids_trip[id_point]][-1]])/60.0
                    if self.select_points == 3:
                        time = (points.timestamps[id_point] - trips.timestamps[points.ids_trip[id_point]])/60.0
                    if self.select_points == 4:
                        time = (points.timestamps[id_point] - trips.timestamps[points.ids_trip[id_point]])/60.0

                    if 0 < time < self.max_time:
                        count[id_closest_edge-1] += 1
                        times_edge[id_closest_edge-1] += time
                        if count[id_closest_edge-1] > min_n_values:
                            values[id_closest_edge-1] = times_edge[id_closest_edge-1]/count[id_closest_edge-1]

                        # For the isochrone plot
# if self.is_isochrone:
##                    times_point[id_point] = time
            print('number of points:', np.sum(count))

            # Plot result on map
            print('plot results on map')
            if self.select_points == 1:
                title = 'Isochrone from the origin zone'
            if self.select_points == 2:
                title = 'Isochrone to the destination zone'
            if self.select_points == 3:
                title = 'Isochrone from the origin to the destination zone'
            if self.select_points == 4:
                title = 'Isochrone for all trips'

            self.plot_results_on_map(ax, ids_edge=ids_edge,
                                     values=values[ids_edge-1],
                                     title=title,
                                     valuelabel='Minutes',
                                     )

            if self.is_save:
                if self.select_points == 1:
                    self.save_fig('routeana_isochrone_fromO')
                if self.select_points == 2:
                    self.save_fig('routeana_isochrone_toD')
                if self.select_points == 3:
                    self.save_fig('routeana_isochrone_FromOtoD')
                if self.select_points == 4:
                    self.save_fig('routeana_isochrone')

        if self.plot_type == 2:

            # Plot heatmap
            x_points = coords[:, 0]
            y_points = coords[:, 1]
            xedges = np.linspace(0., 14000., num=self.bins)
            yedges = np.linspace(0., 11000., num=self.bins)
            extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
            H, xedges, yedges = np.histogram2d(x_points, y_points, bins=(xedges, yedges))
            X, Y = np.meshgrid(xedges, yedges)
            if self.titletext != '':
                ax.set_title(self.titletext, fontsize=self.size_titlefont)
            else:
                if self.select_points == 1:
                    ax.set_title('Density heatmap of trip points from the origin zone', fontsize=self.size_titlefont)
                if self.select_points == 2:
                    ax.set_title('Density heatmap of trip points to the destination zone', fontsize=self.size_titlefont)
                if self.select_points == 3:
                    ax.set_title('Density heatmap of trip points from the origin zone to the destination zone',
                                 fontsize=self.size_titlefont)
                if self.select_points == 4:
                    ax.set_title('Density heatmap of all trip points', fontsize=self.size_titlefont)

            plt.imshow(list(zip(*reversed(list(zip(*reversed(list(zip(*reversed(H))))))))),
                       vmin=self.vmin, cmap='Reds', extent=extent, interpolation='bicubic', alpha=0.9)
            plt.colorbar()
            if self.is_save:
                if self.select_points == 1:
                    self.save_fig('routeana_heatmappoints_fromO')
                if self.select_points == 2:
                    self.save_fig('routeana_heatmappoints_toD')
                if self.select_points == 3:
                    self.save_fig('routeana_heatmappoints_FromOtoD')
                if self.select_points == 4:
                    self.save_fig('routeana_heatmappoints')

        # print isochrone
        if self.is_isochrone:
            times_point = np.zeros(np.max(ids_point)+1)
            for id_point in ids_point:
                if self.select_points == 1:
                    time = (points.timestamps[id_point] - trips.timestamps[points.ids_trip[id_point]])/60.0
                if self.select_points == 2:
                    time = -(points.timestamps[id_point] -
                             points.timestamps[trips.ids_points[points.ids_trip[id_point]][-1]])/60.0
                if self.select_points == 3:
                    time = (points.timestamps[id_point] - trips.timestamps[points.ids_trip[id_point]])/60.0
                if self.select_points == 4:
                    time = (points.timestamps[id_point] - trips.timestamps[points.ids_trip[id_point]])/60.0

                times_point[id_point] = time
            self.plot_isochrone(ax, ids_point, zone_shape_origin, times_point)

        if not self.is_save:
            show_plot()

    def plot_isochrone(self, ax, ids_point, zone_shape, times_point):

        mapmatching = self.parent.parent
        scenario = mapmatching.get_scenario()
        points = scenario.demand.mapmatching.points
        zone_g = np.array([np.mean(np.array(zone_shape)[:, 0]), np.mean(np.array(zone_shape)[:, 1])])
        coords = points.coords[ids_point]
        coords = coords[:][:, :2]
        isochrone = []
        for coord, id_point, time_point in zip(coords,  ids_point, times_point[ids_point]):
            if zone_g[0] < coord[0] and zone_g[1] < coord[1]:
                angle = np.arctan((coord[0]-zone_g[0])/(coord[1]-zone_g[1]))
            if zone_g[0] < coord[0] and zone_g[1] > coord[1]:
                angle = np.arctan((zone_g[1]-coord[1])/(coord[0]-zone_g[0])) + np.pi/2
            if zone_g[0] > coord[0] and zone_g[1] > coord[1]:
                angle = np.arctan((zone_g[0]-coord[0])/(zone_g[1]-coord[1])) + np.pi
            if zone_g[0] > coord[0] and zone_g[1] < coord[1]:
                angle = np.arctan((coord[1]-zone_g[1])/(zone_g[0]-coord[0])) + np.pi*3/2
            dist = np.sqrt(np.sum((coord-zone_g)**2))
            isochrone.append([angle, id_point, time_point, dist])
        isochrone = np.asarray(isochrone)
        isochrone = isochrone[isochrone[:, 0].argsort()]
        isochrone = isochrone[(isochrone[:, 2] > 0)]

        n_bins = 200.0
##        max_perc_var = 20.
        isochrone_times = [5., 10., 15., 20., 25., 30., 35.,  40.]
        isochrone_shapes = [[]]*len(isochrone_times)
        for bin in range(int(n_bins)):
            for i in range(len(isochrone_times)):
                iso_points = isochrone[(bin*2.0*np.pi/n_bins <= isochrone[:, 0]) & (isochrone[:, 0] < (bin+1)
                                                                                    * 2.0*np.pi/n_bins) & (0 < isochrone[:, 2]) & (isochrone_times[i] > isochrone[:, 2])]
                if len(iso_points) > 10:
                    if len(isochrone_shapes[i]) == 0:
                        isochrone_shapes[i] = [[(points.coords[iso_points[np.argmax(iso_points[:, 3]), 1]][0]),
                                                (points.coords[iso_points[np.argmax(iso_points[:, 3]), 1]][1])]]
                    else:
                        isochrone_shapes[i].append([(points.coords[iso_points[np.argmax(iso_points[:, 3]), 1]][0]),
                                                    (points.coords[iso_points[np.argmax(iso_points[:, 3]), 1]][1])])
        print(isochrone_shapes)
        for isochrone_shape in isochrone_shapes:
            verts = np.array(isochrone_shape)[:, :2].tolist()
            verts.append([0, 0])
            codes = [Path.MOVETO]
            for i in range(len(verts)-2):
                codes.append(Path.LINETO)
            codes.append(Path.CLOSEPOLY)
            path = Path(verts, codes)
            facecolor = 'none'
            patch = patche.PathPatch(path, facecolor=facecolor, edgecolor='r', lw=5, alpha=0.8)
            ax.add_patch(patch)

        for isochrone_shape in isochrone_shapes:

            if len(isochrone_shape) > 4:
                zone_shape = isochrone_shape
                print(zone_shape)
                for zone_shape_coords, i in zip(isochrone_shape, range(len(isochrone_shape))):
                    print(i, len(isochrone_shape))
                    if i == 0:
                        zone_shape[i] = ((np.array(isochrone_shape[i])+np.array(isochrone_shape[i+1])+np.array(
                            isochrone_shape[-1])+np.array(isochrone_shape[i+2])+np.array(isochrone_shape[-2]))/5.).tolist()
                    elif i == (len(isochrone_shape)-1):
                        zone_shape[i] = ((np.array(isochrone_shape[i])+np.array(isochrone_shape[0])+np.array(
                            isochrone_shape[i-1])+np.array(isochrone_shape[i-2])+np.array(isochrone_shape[1]))/5.).tolist()
                    elif i == 1:
                        zone_shape[i] = ((np.array(isochrone_shape[i])+np.array(isochrone_shape[i+1])+np.array(
                            isochrone_shape[i-1])+np.array(isochrone_shape[i+2])+np.array(isochrone_shape[-1]))/5.).tolist()
                    elif i == (len(isochrone_shape)-2):
                        zone_shape[i] = ((np.array(isochrone_shape[i])+np.array(isochrone_shape[i+1])+np.array(
                            isochrone_shape[i-1])+np.array(isochrone_shape[i-2])+np.array(isochrone_shape[0]))/5.).tolist()
                    else:
                        zone_shape[i] = ((np.array(isochrone_shape[i])+np.array(isochrone_shape[i+1])+np.array(
                            isochrone_shape[i-1])+np.array(isochrone_shape[i+2])+np.array(isochrone_shape[i-2]))/5.).tolist()

                verts = np.array(zone_shape)[:, :2].tolist()
                verts.append([0, 0])
                codes = [Path.MOVETO]
                for i in range(len(verts)-2):
                    codes.append(Path.LINETO)
                codes.append(Path.CLOSEPOLY)
                path = Path(verts, codes)
                facecolor = 'none'
                patch = patche.PathPatch(path, facecolor=facecolor, edgecolor='b', lw=5, alpha=0.8)
                ax.add_patch(patch)

        return True

    def get_scenario(self):
        return self.parent.get_scenario()


class NoderesultPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Node results plotter',
                 info="Plots nodes related results of GPS trips using matplotlib",
                 logger=None, **kwargs):

        self._init_common('noderesultplotter', parent=results, name=name,
                          info=info, logger=logger)

        # print 'Resultplotter.__init__',results,self.parent
        attrsman = self.get_attrsman()

        self.plotthemefuncs = OrderedDict([
            ('times wait', self.plot_times_wait),
        ])
        self.plottheme = attrsman.add(cm.AttrConf('plottheme', kwargs.get('plottheme', 'times wait'),
                                                  groupnames=['options'],
                                                  choices=self.plotthemefuncs.keys(),
                                                  name='Plot theme',
                                                  info='Theme  or edge attribute to be plottet.',
                                                  ))

        self.n_min_matched = attrsman.add(cm.AttrConf('n_min_matched', 3,
                                                      groupnames=['options'],
                                                      name='Minum number of matched for speed analysis',
                                                      info='Only nodes contained in almost this number of matched routes\
                                        will be considered for plotting the dynamic\
                                        characteristics of edges (speeds and times).',
                                                      ))
        self.add_plotoptions(**kwargs)
        self.add_save_options(**kwargs)

        attrsman.delete('plottype')
        attrsman.delete('resultwidth')
        attrsman.delete('length_arrowhead')
        attrsman.delete('is_widthvalue')

    def plot_all_themes(self):
        for plottheme in self.plotthemefuncs.keys():
            self.plottheme = plottheme
            self.show()
##
# self.is_grid = attrsman.add(cm.AttrConf(  'is_grid', kwargs.get('is_grid', True),
##                                        groupnames = ['options'],
##                                        name = 'Show grid?',
##                                        info = 'If True, shows a grid on the graphical representation.',
# ))
##
# self.titletext = attrsman.add(cm.AttrConf(  'titletext', kwargs.get('titletext', ''),
##                                        groupnames = ['options'],
##                                        name = 'Title text',
##                                        info = 'Title text. Empty text means no title.',
# ))
##
# self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont',32),
##                                        groupnames = ['options'],
##                                        name = 'Title fontsize',
##                                        info = 'Title fontsize.',
# ))
##
# self.size_labelfont = attrsman.add(cm.AttrConf('size_labelfont', kwargs.get('size_labelfont',24),
##                                        groupnames = ['options'],
##                                        name = 'Label fontsize',
##                                        info = 'Label fontsize.',
# ))
##
# self.width_line = attrsman.add(cm.AttrConf(  'width_line', kwargs.get('width_line', 1.0),
##                                        groupnames = ['options'],
##                                        name = 'Line width',
##                                        info = 'Line width of plot.',
# ))
        self.add_save_options()

    def get_noderesults(self):
        return self.parent.nodesresults

    def show(self):
        print('NoderesultPlotter.show', self.plottheme)
        # if self.axis  is None:
        #axis = init_plot()
        self.init_figures()
        fig = self.create_figure()
        axis = fig.add_subplot(111)
        self.plotthemefuncs[self.plottheme](axis)

        print('  self.is_save', self.is_save)
        if not self.is_save:
            print('  show_plot')
            show_plot()
        else:
            figname = 'nodeplot_'+self.plottheme
            # print '  savefig',figname

            # self.save_fig('edgeplot_'+self.plottheme)

            rootfilepath = self.get_scenario().get_rootfilepath()

            fig.savefig("%s_%s.%s" % (rootfilepath, figname, self.figformat),
                        format=self.figformat,
                        dpi=self.resolution,
                        # orientation='landscape',
                        orientation='portrait',
                        transparent=True)
            plt.close(fig)

    def plot_times_wait(self, ax):
        print('show noderesults', len(self.parent.nodesresults))
        # if self.axis  is None:

        nodesresults = self.parent.nodesresults

        if len(nodesresults) == 0:
            return False

        mapmatching = self.parent.parent
        trips = mapmatching.trips
        #points = mapmatching.points
        routes = trips.get_routes()
        scenario = mapmatching.get_scenario()
        edges = scenario.net.edges
        nodes = scenario.net.nodes
        ids_noderes = nodesresults.select_ids(
            nodesresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)
        nodetypes = nodes.types[nodesresults.ids_node[ids_noderes]]
        times_wait = nodesresults.times_wait[ids_noderes]
        nodetypeset = set(nodetypes)

##        map_type_to_typename = get_inversemap(nodes.types.choices)
##        map_typename_to_times_wait = {}
# for thistype in nodetypeset:
##            map_typename_to_times_wait[map_type_to_typename[thistype]] = np.mean(times_wait[nodetypes == thistype])

# self.init_figures()
##        fig = self.create_figure()
##        ax = fig.add_subplot(111)
        self.plot_node_results_on_map(ax, ids_noderes,
                                      # edgesresults.differences_dist_tot_shortest[ids_result]/edgesresults.numbers_tot_shortest[ids_result],
                                      times_wait,
                                      title='Average waiting time at intersections',
                                      valuelabel='Average waiting time [s]',
                                      is_node_results=True,)


# colors = np.array(COLORS,dtype = np.object)
##        inds_plot = np.arange(len(map_typename_to_times_wait), dtype = np.int32)
##        bar_width = 0.45
##        opacity = 0.5
# error_config = {'ecolor': '0.3'}
# print '  colors',get_colors(inds_plot)
# rects = ax.barh( inds_plot,
# map_typename_to_times_wait.values(),
# align='center',
# alpha=opacity,
# height=bar_width,
# color=get_colors(inds_plot),
# yerr=std_women, error_kw=error_config,
##                         linewidth = self.width_line,
# facecolor=colors[inds][inds_nz],
# )
##
##
##        ax.set_yticks( inds_plot + bar_width / 2)
# ax.set_yticklabels(map_typename_to_times_wait.keys())
# ax.legend()
##
# ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)
# ax.grid(self.is_grid)
# if self.titletext != '':
##            ax.set_title(self.titletext, fontsize=self.size_titlefont)
##        ax.set_xlabel('Average wait times [s]', fontsize=self.size_labelfont)
##        ax.set_ylabel('Intersection type', fontsize=self.size_labelfont)
##        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
##        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
# fig.tight_layout()
# if self.is_save:
# self.save_fig('virtualpop_strategy_share_current')
##
##
##
# plt.show()
        # show_plot()

    def get_scenario(self):
        return self.parent.get_scenario()


class SpeedprofilePlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Speedprofile plotter with Matplotlib',
                 info="Plots the speed profile of a selected GPS trip using matplotlib",
                 logger=None, **kwargs):

        self._init_common('speedprofileplotter', parent=results, name=name,
                          info=info, logger=logger)

        # print 'Resultplotter.__init__',results,self.parent
        attrsman = self.get_attrsman()

        self.id_trip = attrsman.add(cm.AttrConf('id_trip', kwargs.get('id_trip', -1),
                                                groupnames=['options'],
                                                name='Trip ID',
                                                info='ID of GPS trip to be plotted.',
                                                ))

        self.is_plot_similar_trips = attrsman.add(cm.AttrConf('is_plot_similar_trips', kwargs.get('is_plot_similar_trips', True),
                                                              groupnames=['options'],
                                                              name='Plot similar trips',
                                                              info='If True, plot all trips which contain the same route as the given trip.',
                                                              ))

        self.method_interp = attrsman.add(cm.AttrConf('method_interp', kwargs.get('method_interp', 'slinear'),
                                                      groupnames=['options'],
                                                      choices=['linear', 'nearest', 'zero',
                                                               'slinear', 'quadratic', 'cubic'],
                                                      name='Interpolation method',
                                                      info='GPS point interpolation method.',
                                                      ))

        self.color_point = attrsman.add(cm.AttrConf('color_point', kwargs.get('color_point', np.array([0.0, 0.4, 0.6, 0.6], np.float32)),
                                                    groupnames=['options'],
                                                    perm='wr',
                                                    metatype='color',
                                                    name='Point color',
                                                    info='Color of GPS-points.',
                                                    ))

        self.size_point = attrsman.add(cm.AttrConf('size_point', kwargs.get('size_point', 10.0),
                                                   groupnames=['options'],
                                                   name='Point size',
                                                   info='Point size of GPS points.',
                                                   ))

        self.is_pointlabel = attrsman.add(cm.AttrConf('is_pointlabel', kwargs.get('is_pointlabel', True),
                                                      groupnames=['options'],
                                                      name='Label points?',
                                                      info='If True, shows point ID nex to each point.',
                                                      ))

        self.is_waitslabel = attrsman.add(cm.AttrConf('is_waitslabel', kwargs.get('is_waitslabel', True),
                                                      groupnames=['options'],
                                                      name='Show wait times?',
                                                      info='If True, shows wait times for each edge.',
                                                      ))

        self.is_waitslabel_junction = attrsman.add(cm.AttrConf('is_waitslabel_junction', kwargs.get('is_waitslabel_junction', True),
                                                               groupnames=['options'],
                                                               name='Show wait times at junctions?',
                                                               info='If True, shows wait times at junctions for each edge entering a junction.',
                                                               ))

        self.is_waitslabel_tls = attrsman.add(cm.AttrConf('is_waitslabel_tls', kwargs.get('is_waitslabel_tls', True),
                                                          groupnames=['options'],
                                                          name='Show wait times at TLS?',
                                                          info='If True, shows wait times at traffic light systems (TLS) for each edge entering a traffic light.',
                                                          ))

        self.color_line = attrsman.add(cm.AttrConf('color_line', kwargs.get('color_line', np.array([1.0, 0.6, 0.0, 1.0], np.float32)),
                                                   groupnames=['options'],
                                                   perm='wr',
                                                   metatype='color',
                                                   name='Line color',
                                                   info='Color of plotted line in diagram.',
                                                   ))

        self.width_line = attrsman.add(cm.AttrConf('width_line', kwargs.get('width_line', 3.0),
                                                   groupnames=['options'],
                                                   name='Line width',
                                                   info='Line width of plot.',
                                                   ))

        self.alpha_line = attrsman.add(cm.AttrConf('alpha_line', kwargs.get('alpha_line', 0.8),
                                                   groupnames=['options'],
                                                   name='Line transp.',
                                                   info='Line transparency of plot.',
                                                   ))

        self.is_grid = attrsman.add(cm.AttrConf('is_grid', kwargs.get('is_grid', True),
                                                groupnames=['options'],
                                                name='Show grid?',
                                                info='If True, shows a grid on the graphical representation.',
                                                ))

        self.titletext = attrsman.add(cm.AttrConf('titletext', kwargs.get('titletext', ''),
                                                  groupnames=['options'],
                                                  name='Title text',
                                                  info='Title text. Empty text means no title.',
                                                  ))

        self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont', 32),
                                                       groupnames=['options'],
                                                       name='Title fontsize',
                                                       info='Title fontsize.',
                                                       ))

        self.size_labelfont = attrsman.add(cm.AttrConf('size_labelfont', kwargs.get('size_labelfont', 24),
                                                       groupnames=['options'],
                                                       name='Label fontsize',
                                                       info='Label fontsize.',
                                                       ))
        self.add_save_options()

    def plot_speed_over_time(self, ax, id_trip, id_route, edges, i_min=None, i_max=None,
                             is_pointlabel=True, alpha=1.0):
        print('plot_speed_over_time', id_trip, type(id_trip), self.parent.parent)

        #mapmatching = self.parent.parent
        #trips = mapmatching.trips

        routeresults = self.get_routeresults()

        #id_route = trips.ids_route_matched[id_trip]
        id_routeres = routeresults.ids_route.get_id_from_index(id_route)

        mapmatching = self.parent.parent
        trips = mapmatching.trips
        ids_point = routeresults.ids_valid_point_speedana[id_routeres]

        # tripresults.pointsposition[id_routeres],\
        # tripresults.pointsspeed[id_routeres],
        # tripresults.pointstime[id_routeres],
        # ids_pointedges,

        if i_min is None:
            #ids_pointedge = routeresults.ids_pointedges[id_routeres]
            #pointspositions = routeresults.pointspositions[id_routeres]
            speeds = routeresults.speedana_point_speeds[id_routeres]
            times = routeresults.speedana_point_times[id_routeres]
        else:
            #ids_pointedge = routeresults.ids_pointedges[id_routeres][i_min:i_max]
            #pointspositions = routeresults.pointspositions[id_routeres][i_min:i_max]
            speeds = routeresults.speedana_point_speeds[id_routeres][i_min:i_max]
            times = routeresults.speedana_point_times[id_routeres][i_min:i_max]
        # print '  id_route,id_routeres',id_route,id_routeres
        # print '  ids_pointedge',ids_pointedge
        n_point = len(times)

        x = np.array(times, dtype=np.float32)
        y = np.array(speeds, dtype=np.float32)*3.6  # in km/h
        #ax = init_plot()
        # print '  x',x
        # print '  y',y
        #ax.plot(locations, speeds, color = self.color_line[:2], lw = self.width_line ,alpha=0.9 ,zorder = 0)

        alpha = min(alpha, self.alpha_line)

        if is_scipy & (not (self.method_interp == 'linear')):

            print('use scipy to interpolate')
            #tck = interpolate.splrep(x, y, s=0)
            #xnew = np.linspace(np.min(x), np.max(x), 200)
            #ynew = interpolate.splev(xnew, tck, der=0)
            # if 1:
            f_inter = interpolate.interp1d(x, y, kind=self.method_interp)
            xnew = np.linspace(np.min(x), np.max(x), 200)
            ynew = f_inter(xnew)

            ax.plot(xnew, ynew, color=self.color_line, lw=self.width_line, alpha=alpha)
            ax.plot(x, y, 'o', markersize=self.size_point, color=self.color_point, alpha=alpha)
        else:
            ax.plot(x, y, 'o-', markersize=self.size_point, color=self.color_line,
                    lw=self.width_line, markerfacecolor=self.color_point, alpha=alpha)

        # label points
        if self.is_pointlabel & is_pointlabel:
            for id_point,  xi, yi in zip(ids_point, x, y):

                ax.text(xi+2, yi, ' %s ' % (str(id_point)),
                        verticalalignment='top',
                        horizontalalignment='left',
                        #rotation = 'vertical',
                        fontsize=int(0.8*self.size_labelfont))  # baseline

    def plot_speed_over_way(self, ax, id_trip, id_route, edges, i_min=None, i_max=None,
                            is_pointlabel=True, alpha=1.0):
        print('plot_speed_over_way', id_trip, type(id_trip), self.parent.parent)

        #mapmatching = self.parent.parent
        #trips = mapmatching.trips

        routeresults = self.get_routeresults()

        #id_route = trips.ids_route_matched[id_trip]
        id_routeres = routeresults.ids_route.get_id_from_index(id_route)

        mapmatching = self.parent.parent
        trips = mapmatching.trips
        ids_point = routeresults.ids_valid_point_speedana[id_routeres]

        # tripresults.pointsposition[id_routeres],\
        # tripresults.pointsspeed[id_routeres],
        # tripresults.pointstime[id_routeres],
        # ids_pointedges,

        if i_min is None:
            ids_pointedge = routeresults.ids_pointedges[id_routeres]
            pointspositions = routeresults.speedana_point_pos[id_routeres]
            speeds = routeresults.speedana_point_speeds[id_routeres]
            times = routeresults.speedana_point_times[id_routeres]
        else:
            ids_pointedge = routeresults.ids_pointedges[id_routeres][i_min:i_max]
            pointspositions = routeresults.speedana_point_pos[id_routeres][i_min:i_max]
            speeds = routeresults.speedana_point_speeds[id_routeres][i_min:i_max]
            times = routeresults.speedana_point_times[id_routeres][i_min:i_max]
        # print '  id_route,id_routeres',id_route,id_routeres
        # print '  ids_pointedge',ids_pointedge
        n_point = len(ids_pointedge)

        alpha = min(alpha, self.alpha_line)
        x = pointspositions
        y = np.array(speeds, dtype=np.float32)*3.6  # in km/h

        #ax = init_plot()
        print('  ids_point', routeresults.ids_valid_point_speedana[id_routeres])
        print('  position', pointspositions)
        print('  x', x)
        print('  y', y)
        #ax.plot(locations, speeds, color = self.color_line[:2], lw = self.width_line ,alpha=0.9 ,zorder = 0)

        if is_scipy & (not (self.method_interp == 'linear')):
            #tck = interpolate.splrep(x, y, s=0)
            #xnew = np.linspace(np.min(x), np.max(x), 200)
            #ynew = interpolate.splev(xnew, tck, der=0)
            # if 1:
            f_inter = interpolate.interp1d(x, y, kind=self.method_interp)
            xnew = np.linspace(np.min(x), np.max(x), 200)
            ynew = f_inter(xnew)

            ax.plot(xnew, ynew, color=self.color_line, lw=self.width_line, alpha=alpha)
            ax.plot(x, y, 'o', markersize=self.size_point, color=self.color_point, alpha=alpha)
        else:
            ax.plot(x, y, 'o-', markersize=self.size_point, color=self.color_line,
                    lw=self.width_line, markerfacecolor=self.color_point, alpha=alpha)
        if self.is_pointlabel & is_pointlabel:
            # label points
            for id_point,  xi, yi in zip(ids_point, x, y):

                ax.text(xi+2, yi, ' %s ' % (str(id_point)),
                        verticalalignment='top',
                        horizontalalignment='left',
                        #rotation = 'vertical',
                        fontsize=int(0.8*self.size_labelfont))  # baseline

    def show(self):
        print('show', self.id_trip, type(self.id_trip), self.parent.parent)
        # if self.axis  is None:

        if self.id_trip >= 0:
            id_trip = self.id_trip
            mapmatching = self.parent.parent
            trips = mapmatching.trips
            #points = mapmatching.points
            routes = trips.get_routes()
            scenario = mapmatching.get_scenario()
            edges = scenario.net.edges
            nodes = scenario.net.nodes

            routeresults = self.get_routeresults()

            if id_trip in trips:
                id_route = trips.ids_route_matched[id_trip]
            else:
                return False

            route = routes.ids_edges[id_route]

            if routeresults.ids_route.has_index(id_route):
                id_routeres = routeresults.ids_route.get_id_from_index(id_route)
            else:
                return False

            edgesresults = self.parent.edgesresults
            connectionsresults = self.parent.connectionsresults

            # tripresults.pointsposition[id_routeres],\
            # tripresults.pointsspeed[id_routeres],
            # tripresults.pointstime[id_routeres],
            # ids_pointedges,
            self.init_figures()
            fig = self.create_figure()
            ax = fig.add_subplot(111)

            fig2 = self.create_figure()
            ax2 = fig2.add_subplot(111)

            self.plot_speed_over_way(ax, id_trip, id_route, edges)
            self.plot_speed_over_time(ax2, id_trip, id_route, edges)

            # get_color()
            # is_sublist
            #id_route = trips.ids_route_matched[id_trip]
            #route = routes.ids_edge[id_route]
            ids_pointedge = routeresults.ids_pointedges[id_routeres]
            if self.is_plot_similar_trips:
                #id_routeres = routeresults.ids_route.get_ids_from_indices(ids_route)

                id_pointedge_first = ids_pointedge[0]
                id_pointedge_last = ids_pointedge[-1]

                ids_routeres_speed = routeresults.get_ids()
                ids_route_speed = routeresults.ids_route[ids_routeres_speed]
                # print '  route',route
                for id_trip_speed, id_route_speed, route_speed, ids_pointedge_speed in zip(
                        routes.ids_trip[ids_route_speed],
                        ids_route_speed, routes.ids_edges[ids_route_speed],
                        routeresults.ids_pointedges[ids_routeres_speed]):
                    # print '    ids_pointedge_speed',ids_pointedge_speed
                    # print '    route[0],is_inlist',route[0],ids_pointedge_speed.count(route[0]),type(ids_pointedge_speed)
                    # print '    route_speed',route_speed

                    # is_sublist(route,route_speed):# |  is_sublist(route_speed,route):
                    if is_sublist(route_speed, route):
                        i = ids_pointedge_speed.index(id_pointedge_first)
                        j = ids_pointedge_speed.index(id_pointedge_last)
                        n_pointedge = len(ids_pointedge_speed)
                        while (ids_pointedge_speed[j] == id_pointedge_last) & (j < n_pointedge-1):
                            j += 1
                        self.plot_speed_over_way(ax, id_trip_speed, id_route_speed, edges,
                                                 i, j, is_pointlabel=False, alpha=0.5)
                        self.plot_speed_over_time(ax2, id_trip_speed, id_route_speed, edges,
                                                  i, j, is_pointlabel=False, alpha=0.5)

            # plot edge info to speed over time

            colors = [(0.2, 0.2, 0.2, 0.7), (0.8, 0.8, 0.8, 0.7)]
            ymin, ymax = ax.get_ylim()
            x = 0
            x_last = routeresults.speedana_point_pos[id_routeres][0]
            t_last = 0
            i = 0
##            id_arc_last = routeresults.ids_arc_point[id_routeres][0]
            id_arc_last = 0
            i_point = 0
            pointstime = routeresults.speedana_point_times[id_routeres]
            print('  len(ids_pointedge)', len(ids_pointedge))
            print('  len(pointstime)', len(pointstime))

            # if len(pointstime)>1:
            #    t_last = pointstime[1]
            # else:
            #t_last_edge = pointstime[0]
            #t_last = pointstime[0]
            # print '  ids_pointedge\n',ids_pointedge
            # print '  pointstime\n',pointstime
            # print '  pointsspeeds\n',routeresults.pointsspeeds[id_routeres]

            linestyle = '--'

            for id_arc_point, is_connection_point, t, v, pos in zip(
                routeresults.ids_arc_point[id_routeres],
                routeresults.is_connection_point[id_routeres],
                pointstime,
                routeresults.speedana_point_speeds[id_routeres],
                routeresults.speedana_point_pos[id_routeres],
            ):

                print('  id_arc_point', id_arc_point, 'is_connection_point', is_connection_point, 'id_arc_last', id_arc_last, id_arc_point != id_arc_last, 'pos=%.2fm, t=%.2fs, v=%.2fkm/h' % (pos, t, v*3.6),)
                color = 'k'
                if (id_arc_point != id_arc_last):

                    t_last = t  # memorize t where edge ids changed
                    if not is_connection_point:
                        # edges have changed
                        # if id_edge != -1:
                        #    x += edges.lengths[id_edge] # add length of old id_edge

                        ax2.text(t_last+2, ymax, ' ID Edge = %s ' % (str(id_arc_point)),
                                 verticalalignment='top',
                                 horizontalalignment='left',
                                 rotation='vertical',
                                 fontsize=int(0.8*self.size_labelfont))  # baseline

                        # put wait times, if any available
                        if edgesresults.ids_edge.has_index(id_arc_point):
                            id_edgeres = edgesresults.ids_edge.get_id_from_index(id_arc_point)
                            time_wait = edgesresults.times_wait[id_edgeres]
                            time_wait_junc = edgesresults.times_wait_junc[id_edgeres]
                            time_wait_tls = edgesresults.times_wait_tls[id_edgeres]
                            label = r'  '
                            # is_waitslabel,is_waitslabel_junction,is_waitslabel_tls
                            if self.is_waitslabel & (time_wait > 0):
                                label += ' $T_W=%ds$' % time_wait

                            if self.is_waitslabel_junction & (time_wait_junc > 0):
                                label += ' $T_J=%ds$' % time_wait_junc

                            if self.is_waitslabel_tls & (time_wait_tls > 0):
                                label += ' $T_{\mathrm{TL}}=%ds$' % time_wait_tls

                            ax2.text(t_last+2, ymin, label,
                                     verticalalignment='bottom',
                                     horizontalalignment='left',
                                     rotation='vertical',
                                     color=color,
                                     fontsize=int(0.8*self.size_labelfont))

                        ax2.plot([t_last, t_last], [ymin, ymax], color=color, linestyle=linestyle)
                        t_last = t  # memorize t where edge ids changed

                    else:
                        # edges have changed
                        # if id_edge != -1:
                        #    x += edges.lengths[id_edge] # add length of old id_edge

                        ax2.text(t_last+2, ymax, ' ID Connection = %s ' % (str(id_arc_point)),
                                 verticalalignment='top',
                                 horizontalalignment='left',
                                 rotation='vertical',
                                 fontsize=int(0.8*self.size_labelfont))  # baseline

                        # put wait times, if any available
                        if connectionsresults.ids_connection.has_index(id_arc_point):
                            id_connectionres = connectionsresults.ids_connection.get_id_from_index(id_arc_point)
                            time_wait = connectionsresults.times_wait[id_connectionres]
##                            time_wait_junc = connectionsresults.times_wait_junc[id_edgeres]
                            time_wait_tls = connectionsresults.times_wait_tls[id_connectionres]
                            label = r'  '
                            # is_waitslabel,is_waitslabel_junction,is_waitslabel_tls
                            if self.is_waitslabel & (time_wait > 0):
                                label += ' $T_W=%ds$' % time_wait

##
# if self.is_waitslabel_junction & (time_wait_junc>0):
##                                label += ' $T_J=%ds$'%time_wait_junc
##

                            if self.is_waitslabel_tls & (time_wait_tls > 0):
                                label += ' $T_{\mathrm{TL}}=%ds$' % time_wait_tls

                            ax2.text(t_last+2, ymin, label,
                                     verticalalignment='bottom',
                                     horizontalalignment='left',
                                     rotation='vertical',
                                     color=color,
                                     fontsize=int(0.8*self.size_labelfont))

                        ax2.plot([t_last, t_last], [ymin, ymax], color=color, linestyle=linestyle)

                id_arc_last = id_arc_point  # memorize edge of last point

                i_point += 1

            #ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)
            ax2.grid(self.is_grid)
            if self.titletext != '':
                ax2.set_title(self.titletext, fontsize=self.size_titlefont)

            ax2.set_xlabel('Time [s]', fontsize=self.size_labelfont)
            ax2.set_ylabel('Speed [km/h]', fontsize=self.size_labelfont)
            ax2.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
            ax2.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

            # plot edge info to speed over way graph
            # this will put correct edge borders and labels
            linestyle = '--'
            color = 'k'
            cumulative_dists = routeresults.cumulative_dists[id_routeres]
            cumulative_dists = np.delete(cumulative_dists, -1)
            cumulative_dists = np.insert(cumulative_dists, 0, 0.).tolist()
            ids_arc = routeresults.ids_arc[id_routeres]
            are_connections = routeresults.is_connection[id_routeres]
            print(cumulative_dists, ids_arc)

            for cumulative_dist, id_arc, is_connection in zip(cumulative_dists, ids_arc, are_connections):

                #ax.plot([x,x+length],[ymin,ymin],color = colors[i%2],lw = 3*self.width_line)

                # ax.text( dist, ymax, ' ID= %s '%(str(id_arc)),
                ##                                        verticalalignment = 'top',
                ##                                        horizontalalignment = 'left',
                ##                                        rotation = 'vertical',
                # fontsize = int(0.8*self.size_labelfont))#baseline

                if not is_connection:
                    if edgesresults.ids_edge.has_index(id_arc):
                        id_edgeres = edgesresults.ids_edge.get_id_from_index(id_arc)
                        time_wait = edgesresults.times_wait[id_edgeres]
                        time_wait_junc = edgesresults.times_wait_junc[id_edgeres]
                        time_wait_tls = edgesresults.times_wait_tls[id_edgeres]
                        label = r'  '

                        if self.is_waitslabel & (time_wait > 0):
                            label += ' $T_W=%ds$' % time_wait

                        if self.is_waitslabel_junction & (time_wait_junc > 0):
                            label += ' $T_J=%ds$' % time_wait_junc

                        if self.is_waitslabel_tls & (time_wait_tls > 0):
                            label += ' $T_{\mathrm{TL}}=%ds$' % time_wait_tls

                        print('  id_edge', id_arc, 'pos=%df' % cumulative_dist, 'time_wait', time_wait, 'time_wait_junc', time_wait_junc, 'time_wait_tls', time_wait_tls)

                        ax.text(cumulative_dist, ymin, label,
                                verticalalignment='bottom',
                                horizontalalignment='left',
                                rotation='vertical',
                                color=color,
                                fontsize=int(0.8*self.size_labelfont))
                    else:
                        print('the edge', id_arc, 'is not in the edgeresult database')
                else:
                    if connectionsresults.ids_connection.has_index(id_arc):
                        id_connectionres = connectionsresults.ids_connection.get_id_from_index(id_arc)
                        time_wait = connectionsresults.times_wait[id_connectionres]
    ##                        time_wait_junc = connectionsresults.times_wait_junc[id_connectionres]
                        time_wait_tls = connectionsresults.times_wait_tls[id_connectionres]
                        label = r'  '

                        if self.is_waitslabel & (time_wait > 0):
                            label += ' $T_W=%ds$' % time_wait

    # if self.is_waitslabel_junction & (time_wait_junc>0):
    ##                            label += ' $T_J=%ds$'%time_wait_junc

                        if self.is_waitslabel_tls & (time_wait_tls > 0):
                            label += ' $T_{\mathrm{TL}}=%ds$' % time_wait_tls

                        print('  id_connection', id_arc, 'pos=%df' % x, 'time_wait', time_wait, 'time_wait_junc', time_wait_junc, 'time_wait_tls', time_wait_tls)

                        ax.text(cumulative_dist, ymin, label,
                                verticalalignment='bottom',
                                horizontalalignment='left',
                                rotation='vertical',
                                color=color,
                                fontsize=int(0.8*self.size_labelfont))
                    else:
                        print('the connection', id_arc, 'is not in the connectionresult database')

                ax.plot([cumulative_dist, cumulative_dist], [ymin, ymax], color=color, linestyle=linestyle)

                if not is_connection:
                    ax.text(cumulative_dist, ymax, ' ID Edge= %s ' % (str(id_arc)),
                            verticalalignment='top',
                            horizontalalignment='left',
                            rotation='vertical',
                            fontsize=int(0.8*self.size_labelfont))  # baseline
                else:
                    ax.text(cumulative_dist, ymax, ' ID Connection= %s ' % (str(id_arc)),
                            verticalalignment='top',
                            horizontalalignment='left',
                            rotation='vertical',
                            fontsize=int(0.8*self.size_labelfont))  # baseline
##                    x += edges.lengths[id_edge]

            #ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)
            ax.grid(self.is_grid)
            if self.titletext != '':
                ax.set_title(self.titletext, fontsize=self.size_titlefont)

            ax.set_xlabel('Distance [m]', fontsize=self.size_labelfont)
            ax.set_ylabel('Speed [km/h]', fontsize=self.size_labelfont)
            ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
            ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

            # if self.is_save:
            #    self.save_fig('routeana_speedprofile')

            plt.show()
            # show_plot()

    def get_routeresults(self):
        return self.parent.routesresults_matched

    def get_scenario(self):
        return self.parent.get_scenario()


class EdgeresultPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Plot edge results with Matplotlib',
                 info="Creates plots of different edge results using matplotlib",
                 logger=None, **kwargs):

        self._init_common('routeresultplotter', parent=results, name=name,
                          info=info, logger=logger)

        # print 'Resultplotter.__init__',results,self.parent
        attrsman = self.get_attrsman()

        self.plotthemefuncs = OrderedDict([
            ('average slopes', self.plot_average_slopes),
            ('positive climbs', self.plot_positive_climbs),
            ('negative climbs', self.plot_negative_climbs),
            ('average speeds', self.plot_speeds_average),
            ('inmove speeds', self.plot_speeds_inmotion),
            ('times wait', self.plot_times_wait),
            ('times wait_tls', self.plot_times_wait_tls),
            ('number matched_routes', self.plot_numbers_tot_matched),
            ('number shortest', self.plot_numbers_tot_shortest),
            ('total deviation', self.plot_differences_dist_tot_shortest),
            ('relative deviation', self.plot_differences_dist_rel_shortest),
            ('usage probabilities by matched routes', self.plot_probabilities_tot_matched),
            ('estimated flows from matched routes', self.plot_flows_est_matched_routes),
        ])
        self.plottheme = attrsman.add(cm.AttrConf('plottheme', kwargs.get('plottheme', 'average speeds'),
                                                  groupnames=['options'],
                                                  choices=self.plotthemefuncs.keys(),
                                                  name='Plot theme',
                                                  info='Theme  or edge attribute to be plottet.',
                                                  ))
        self.n_min_matched = attrsman.add(cm.AttrConf('n_min_matched', 3,
                                                      groupnames=['options'],
                                                      name='Minum number of matched for speed analysis',
                                                      info='Only edge contained in almost this number of matched routes\
                                        will be considered for plotting the dynamic\
                                        characteristics of edges (speeds and times).',
                                                      ))

        self.add_plotoptions(**kwargs)
        self.add_save_options(**kwargs)

    def plot_all_themes(self):
        for plottheme in self.plotthemefuncs.keys():
            self.plottheme = plottheme
            self.show()

    def show(self):
        print('EdgeresultPlotter.show', self.plottheme)
        # if self.axis  is None:
        #axis = init_plot()
        self.init_figures()
        fig = self.create_figure()
        axis = fig.add_subplot(111)
        self.plotthemefuncs[self.plottheme](axis)

        print('  self.is_save', self.is_save)
        if not self.is_save:
            print('  show_plot')
            show_plot()
        else:
            figname = 'edgeplot_'+self.plottheme
            # print '  savefig',figname

            # self.save_fig('edgeplot_'+self.plottheme)

            rootfilepath = self.get_scenario().get_rootfilepath()

            fig.savefig("%s_%s.%s" % (rootfilepath, figname, self.figformat),
                        format=self.figformat,
                        dpi=self.resolution,
                        # orientation='landscape',
                        orientation='portrait',
                        transparent=True)
            plt.close(fig)

    def get_edgeresults(self):
        return self.parent.edgesresults  # must have attribute 'ids_edge'

    def plot_average_slopes(self, ax):

        edges = self.parent.parent.parent.parent.net.edges
        ids_edge = edges.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        average_slopes = edges.average_slopes
        print(ids_edge, average_slopes[ids_edge])
        self.plot_results_on_map(ax,
                                 values=average_slopes[ids_edge],
                                 ids_edge=ids_edge,
                                 title='Average edge slopes',
                                 valuelabel='Slope',
                                 )

    def plot_positive_climbs(self, ax):

        edges = self.parent.parent.parent.parent.net.edges
        ids_edge = edges.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        positive_climbs = edges.positive_climbs
        print(ids_edge, positive_climbs[ids_edge])
        self.plot_results_on_map(ax,
                                 values=positive_climbs[ids_edge],
                                 ids_edge=ids_edge,
                                 title='Average edge slopes',
                                 valuelabel='Slope',
                                 )

    def plot_negative_climbs(self, ax):

        edges = self.parent.parent.parent.parent.net.edges
        ids_edge = edges.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        negative_climbs = edges.negative_climbs
        print(ids_edge, negative_climbs[ids_edge])
        self.plot_results_on_map(ax,
                                 values=negative_climbs[ids_edge],
                                 ids_edge=ids_edge,
                                 title='Average edge slopes',
                                 valuelabel='Slope',
                                 )

    def plot_differences_dist_rel_shortest(self, ax):
        edgesresults = self.get_edgeresults()
        ids_result = edgesresults.select_ids(edgesresults.differences_dist_rel_shortest.get_value() > 0)

        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)

        self.plot_results_on_map(ax, ids_result,
                                 # edgesresults.differences_dist_tot_shortest[ids_result]/edgesresults.numbers_tot_shortest[ids_result],
                                 edgesresults.differences_dist_rel_shortest[ids_result],
                                 title='Deviation generated per trip',
                                 valuelabel='Generated deviation per trip [m]',
                                 )

    def plot_differences_dist_tot_shortest(self, ax):
        edgesresults = self.get_edgeresults()
        ids_result = edgesresults.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        #deviation_tot = edgesresults.differences_dist_tot_shortest
        self.plot_results_on_map(ax, ids_result,
                                 edgesresults.differences_dist_tot_shortest[ids_result]/1000,
                                 # deviation_tot[ids_result]/1000,
                                 title='Total deviation generated per edge',
                                 valuelabel='Generated total deviation [km]',
                                 )

    def plot_numbers_tot_shortest(self, ax):
        edgesresults = self.get_edgeresults()
        ids_result = edgesresults.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        numbers_tot_shortest = edgesresults.numbers_tot_shortest
        self.plot_results_on_map(ax, ids_result,
                                 numbers_tot_shortest[ids_result],
                                 title='Edge usage from shortest routes',
                                 valuelabel='Usage in number of persons',
                                 )

    def plot_numbers_tot_matched(self, ax):
        edgesresults = self.get_edgeresults()
        ids_result = edgesresults.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        numbers_tot_matched = edgesresults.numbers_tot_matched
        self.plot_results_on_map(ax, ids_result,
                                 numbers_tot_matched[ids_result],
                                 title='Edge usage from matched routes',
                                 valuelabel='Usage in number of persons',
                                 )

    def plot_speeds_average(self, ax):
        edgesresults = self.parent.edgesresults

        print('plot_speeds_average')

        #ids_result = edgesresults.get_ids()
        ids_result = edgesresults.select_ids(
            edgesresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)

        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        speeds_average = self.parent.edgesresults.speed_average
        # print '  speeds_average',speeds_average[ids_result]
        # print '  ids_result',ids_result

        self.plot_results_on_map(ax, ids_result,
                                 speeds_average[ids_result]*3.6,
                                 title='Average edge speeds',
                                 valuelabel='Average edge speeds [km/h]',
                                 )

    def plot_speeds_inmotion(self, ax):
        edgesresults = self.parent.edgesresults
        #ids_result = edgesresults.get_ids()
        ids_result = edgesresults.select_ids(
            edgesresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)

        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        speeds = self.parent.edgesresults.speed_average_in_motion

        # print '  speeds_average',speeds[ids_result]
        # print '  ids_result',ids_result

        self.plot_results_on_map(ax, ids_result,
                                 speeds[ids_result]*3.6,
                                 title='Average edge speeds in motion',
                                 valuelabel='Average edge speeds in motion [km/h]',
                                 )

    def plot_times_wait(self, ax):
        edgesresults = self.parent.edgesresults
        #ids_result = edgesresults.get_ids()
        ids_result = edgesresults.select_ids(
            edgesresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)

        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        times = edgesresults.times_wait
        self.plot_results_on_map(ax, ids_result,
                                 times[ids_result],
                                 title='Average wait times',
                                 valuelabel='Average wait times [s]',
                                 )

    def plot_times_wait_tls(self, ax):
        #ids_result = self.parent.edgesresults.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        edgesresults = self.parent.edgesresults
        ids_result = edgesresults.select_ids(
            edgesresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)
        times = edgesresults.times_wait_tls
        self.plot_results_on_map(ax, ids_result,
                                 times[ids_result],
                                 title='Average wait times at Traffic Lights',
                                 valuelabel='Average wait times at TLS [s]',
                                 )

    def plot_probabilities_tot_matched(self, ax):
        ids_result = self.parent.edgesresults.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        self.plot_results_on_map(ax, ids_result,
                                 self.parent.edgesresults.probabilities_tot_matched[ids_result],
                                 title='Probabilities',
                                 valuelabel=r'Enter probabilities [\%]',
                                 )

    def plot_flows_est_matched_routes(self, ax):
        ids_result = self.parent.edgesresults.get_ids()
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        self.plot_results_on_map(ax, ids_result,
                                 self.parent.edgesresults.flows_est[ids_result],
                                 title='Flows',
                                 valuelabel=r'Estimated flows [1/h]',
                                 )

    def do(self):
        # print 'do',self.edgeattrname
        self.show()
        return True


class ConnectionresultPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Plot connection results with Matplotlib',
                 info="Creates plots of different connection results using matplotlib",
                 logger=None, **kwargs):

        self._init_common('routeresultplotter', parent=results, name=name,
                          info=info, logger=logger)

        # print 'Resultplotter.__init__',results,self.parent
        attrsman = self.get_attrsman()

        self.plotthemefuncs = OrderedDict([
            ('times wait', self.plot_times_wait),
            ('times wait_tls', self.plot_times_wait_tls),
        ])
        self.plottheme = attrsman.add(cm.AttrConf('plottheme', kwargs.get('plottheme', 'times wait'),
                                                  groupnames=['options'],
                                                  choices=self.plotthemefuncs.keys(),
                                                  name='Plot theme',
                                                  info='Theme  or edge attribute to be plottet.',
                                                  ))

        self.n_min_matched = attrsman.add(cm.AttrConf('n_min_matched', 3,
                                                      groupnames=['options'],
                                                      name='Minum number of matched for speed analysis',
                                                      info='Only connectors contained in almost this number of matched routes\
                                        will be considered for plotting the dynamic\
                                        characteristics of edges (speeds and times).'))

        self.add_plotoptions(**kwargs)
        self.add_save_options(**kwargs)

    def plot_all_themes(self):
        for plottheme in self.plotthemefuncs.keys():
            self.plottheme = plottheme
            self.show()

    def show(self):
        print('connectionresultPlotter.show', self.plottheme)
        # if self.axis  is None:
        #axis = init_plot()
        self.init_figures()
        fig = self.create_figure()
        axis = fig.add_subplot(111)
        self.plotthemefuncs[self.plottheme](axis)

        print('  self.is_save', self.is_save)
        if not self.is_save:
            print('  show_plot')
            show_plot()
        else:
            figname = 'connectionplot_'+self.plottheme
            # print '  savefig',figname

            # self.save_fig('edgeplot_'+self.plottheme)

            rootfilepath = self.get_scenario().get_rootfilepath()

            fig.savefig("%s_%s.%s" % (rootfilepath, figname, self.figformat),
                        format=self.figformat,
                        dpi=self.resolution,
                        # orientation='landscape',
                        orientation='portrait',
                        transparent=True)
            plt.close(fig)

    def get_connectionresults(self):
        return self.parent.connectionsresults

    def plot_times_wait(self, ax):

        connectionsresults = self.parent.connectionsresults
        #ids_result = edgesresults.get_ids()

        ids_result = connectionsresults.select_ids(
            connectionsresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)
        #resultattrconf = getattr(self.parent.edgesresults, self.edgeattrname)
        times = connectionsresults.times_wait
# print 'TIMES VALUES', times[ids_result]
        self.plot_results_on_map(ax,
                                 values=times[ids_result],
                                 title='Average wait times at connectors',
                                 valuelabel='Average wait times [s]',
                                 is_connection_results=True,
                                 ids_connectionres=ids_result)

    def plot_times_wait_tls(self, ax):

        connectionsresults = self.parent.connectionsresults
        ids_result = connectionsresults.select_ids(
            connectionsresults.numbers_matched_for_speed_analysis.get_value() > self.n_min_matched)
        times = connectionsresults.times_wait_tls
# print 'TIMES VALUES', times[ids_result]
        self.plot_results_on_map(ax,
                                 values=times[ids_result],
                                 title='Average wait times at tls connectors',
                                 valuelabel='Average wait times [s]',
                                 is_connection_results=True,
                                 ids_connectionres=ids_result)

    def do(self):
        # print 'do',self.edgeattrname
        self.show()
        return True


class AlternativeRoutesPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Plot edge of alternative routes with Matplotlib',
                 info="Creates plots of different edge results using matplotlib",
                 logger=None, **kwargs):

        self._init_common('routeresultplotter', parent=results, name=name,
                          info=info, logger=logger)

        # print 'Resultplotter.__init__',results,self.parent
        attrsman = self.get_attrsman()

        self.color_chosen = attrsman.add(cm.AttrConf('color_chosen', kwargs.get('color_chosen', np.array([0.9, 0.2, 0.2, 0.99], dtype=np.float32)),
                                                     groupnames=['options'],
                                                     perm='wr',
                                                     metatype='color',
                                                     name='Color Alt. chosen',
                                                     info='Color of chosen alternative.',
                                                     ))

        self.color2 = attrsman.add(cm.AttrConf('color2', kwargs.get('color2', np.array([0.2, 0.9, 0.2, 0.99], dtype=np.float32)),
                                               groupnames=['options'],
                                               perm='wr',
                                               metatype='color',
                                               name='Color Alt. 2',
                                               info='Color of second alternative.',
                                               ))

        kwargs_fixed = {'plottype': 'polygons',
                        'is_widthvalue': True,
                        'is_colorvalue': False,  # colors assigned explicitely
                        'is_value_range': True,
                        'value_range_min': 0,
                        'value_range_max': 2,
                        'color_fill': np.array([0.9, 0.2, 0.2, 0.99], dtype=np.float32),
                        'printformat': '',
                        }
        kwargs.update(kwargs_fixed)

        self.add_plotoptions(**kwargs)
        self.add_save_options(**kwargs)

        # configure fixed options as privat (non visible for gui)
        attrsman = self.get_attrsman()
        for attrname in kwargs_fixed.keys():
            attrconf = attrsman.get_config(attrname)
            attrconf.add_groupnames(['_private'])

    def show(self):
        print('AlternativeRoutesPlotter.show')
        # if self.axis  is None:
        #axis = init_plot()
        title = 'Route alternatives'
        valuelabel = ''
        self.init_figures()
        fig = self.create_figure()
        axis = fig.add_subplot(111)

        altroutesresults = self.parent.altroutesresults
        ids_res = altroutesresults.get_ids()
        net = self.parent.get_scenario().net
        edges = net.edges

        axis.set_axis_bgcolor(self.color_background)
        if self.is_show_network:
            plot_net(axis, net, color_edge=self.color_network, width_edge=2,
                     color_node=self.color_nodes, alpha=self.alpha_net)

        if self.is_show_facilities:
            facilities = self.parent.get_scenario().landuse.facilities
            plot_facilities(axis, facilities, color_building=self.color_facilities,
                            color_outline=self.color_borders,
                            width_line=2, alpha=self.alpha_facilities,
                            )
        if self.is_show_maps:
            plot_maps(axis, self.parent.get_scenario().landuse.maps, alpha=self.alpha_maps)

        # if self.is_value_range:
        #    value_range = (self.value_range_min, self.value_range_max)
        # else:
        value_range = (0, 2)

        for id_trip, id_choice, chosen, ids_edge_all in zip(
                altroutesresults.ids_trip[ids_res],
                altroutesresults.ids_alt[ids_res],
                altroutesresults.choices[ids_res],
                altroutesresults.ids_edges[ids_res]):
            # print '  id_trip',id_trip,'ids_edge',ids_edge_all
            if ids_edge_all is not None:
                if id_choice == 1:
                    ids_edge = ids_edge_all

                else:
                    ids_edge = ids_edge_all[1:-1]

                if chosen:
                    color = self.color_chosen  # use predefined color sor selected
                elif id_choice == 2:
                    color = self.color2
                else:
                    color = get_color(id_choice)

                plot_edgevalues_lines(axis,
                                      ids_edge=ids_edge,
                                      values=np.ones(len(ids_edge), dtype=np.float32)*(int(chosen)+1),
                                      edges=net.edges,
                                      width_max=self.resultwidth,
                                      alpha=self.alpha_results,
                                      printformat=self.printformat,
                                      color_outline=self.color_outline,
                                      color_fill=color,
                                      color_label=self.color_label,
                                      is_antialiased=True,
                                      is_fill=self.is_colorvalue,
                                      is_widthvalue=self.is_widthvalue,
                                      length_arrowhead=self.length_arrowhead,
                                      fontsize=self.size_labelfont,
                                      valuelabel=False,
                                      value_range=value_range,
                                      is_colorbar=False,
                                      )
            else:
                print('WARNING in AlternativeRoutesPlotter.show ids_edge=', ids_edge_all, 'of id_trip', id_trip)

        if self.is_show_title:
            axis.set_title(title, fontsize=self.size_titlefont)

        axis.axis('equal')
        # ax.legend(loc='best',shadow=True)

        axis.grid(self.is_grid)
        axis.set_xlabel('West-East [m]', fontsize=self.size_labelfont)
        axis.set_ylabel('South-North [m]', fontsize=self.size_labelfont)
        axis.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        axis.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ## save or show
        print('  self.is_save', self.is_save)
        if not self.is_save:
            print('  show_plot')
            show_plot()
        else:
            self.save_fig('altroutesplot')
            plt.close(fig)


class RouteresultPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='Plot route results with Matplotlib',
                 info="Creates plots of different route results using matplotlib",
                 logger=None, **kwargs):

        self._init_common('routeresultplotter', parent=results, name=name,
                          info=info, logger=logger)

        print('Resultplotter.__init__', results, self.parent)
        attrsman = self.get_attrsman()

        mapmatching = self.parent.parent
        scenario = mapmatching.get_scenario()
        self.zones = scenario.landuse.zones
        self.is_plot_bike_availability_ffbss_zone = attrsman.add(cm.AttrConf('is_plot_bike_availability_ffbss_zone', kwargs.get('is_plot_bike_availability_ffbss_zone', True),
                                                                             groupnames=['options'],
                                                                             name='Plot bike relative availability for FFBSS in a zone',
                                                                             info='Plot the variation of the bike availability in a day for a zone. Either 24 or 96 bins are recommended',
                                                                             ))

        if len(self.zones) > 0:

            self.zone_ffbss = attrsman.add(cm.AttrConf('zone_ffbss', kwargs.get('zone_ffbss', self.zones.ids_sumo[1]),
                                                       groupnames=['options'],
                                                       choices=self.zones.ids_sumo,
                                                       name='Zone for the FFBSS availability analysis',
                                                       info='Select the zone for the FFBSS availability analysis.',
                                                       ))

        self.is_analyze_bike_availability_ffbss_zones = attrsman.add(cm.AttrConf('is_analyze_bike_availability_ffbss_zones', kwargs.get('is_analyze_bike_availability_ffbss_zones', True),
                                                                                 groupnames=['options'],
                                                                                 name='Analize bike relative availability for FFBSS in all zones',
                                                                                 info='Plot the absolute and relative (for zones with almost n_bins trips) variation of the bike availability in a day for all zones. Either 24 or 96 bins are recommended',
                                                                                 ))

        self.is_plot_bike_availability_ffbss = attrsman.add(cm.AttrConf('is_plot_bike_availability_ffbss', kwargs.get('is_plot_bike_availability_ffbss', False),
                                                                        groupnames=['options'],
                                                                        name='Plot bike usage distribution',
                                                                        info='Plot the number of bikes that are doing a trip simultaneously in the day period. It require an high number of bins',
                                                                        ))

        self.is_plot_deptimedistrib = attrsman.add(cm.AttrConf('is_plot_deptimedistrib', kwargs.get('is_plot_deptimedistrib', False),
                                                               groupnames=['options'],
                                                               name='Plot departure time distribution',
                                                               info='Plot cumulative distribution on trip departure time of matched route.',
                                                               ))

        # comprison matched shortest
        self.is_plot_lengthdistrib = attrsman.add(cm.AttrConf('is_plot_lengthdistrib', kwargs.get('is_plot_lengthdistrib', True),
                                                              groupnames=['options'],
                                                              name='Plot length distribution',
                                                              info='Plot cumulative distribution on length of matched route and shortest route.',
                                                              ))

        self.is_plot_lengthprob = attrsman.add(cm.AttrConf('is_plot_lengthprob', kwargs.get('is_plot_lengthprob', False),
                                                           groupnames=['options'],
                                                           name='Plot length probabilities',
                                                           info='Plot probabilities length of matched route and shortest route.',
                                                           ))

        self.is_plot_lengthdistrib_by_class = attrsman.add(cm.AttrConf('is_plot_lengthdistrib_by_class', kwargs.get('is_plot_lengthdistrib_by_class', False),
                                                                       groupnames=['options'],
                                                                       name='Plot class length distribution',
                                                                       info='Plot mean values of length of matched route and shortest route for different trip length classes.',
                                                                       ))

        self.distance_class = attrsman.add(cm.AttrConf('distance_class', kwargs.get('distance_class', 2000),
                                                       groupnames=['options'],
                                                       name='Class distance',
                                                       info='Distance to generate trip length classes.',
                                                       ))

        self.is_plot_lengthratio = attrsman.add(cm.AttrConf('is_plot_lengthratio', kwargs.get('is_plot_lengthratio', False),
                                                            groupnames=['options'],
                                                            name='Plot length ratio',
                                                            info='Plot  cumulative distribution  on length ratio between shortest route and matched route.',
                                                            ))

        self.is_plot_lengthoverlap = attrsman.add(cm.AttrConf('is_plot_lengthoverlap', kwargs.get('is_plot_lengthoverlap', False),
                                                              groupnames=['options'],
                                                              name='Plot overlap with shortest',
                                                              info='Plot  cumulative distribution on the realtive length overlap between shortest route and matched route.',
                                                              ))

        self.is_plot_lengthoverlap_fastest = attrsman.add(cm.AttrConf('is_plot_lengthoverlap_fastest', kwargs.get('is_plot_lengthoverlap_fastest', False),
                                                                      groupnames=['options'],
                                                                      name='Plot overlap with fastest',
                                                                      info='Plot  cumulative distribution  on on the realtive length  between fastest route and matched route.',
                                                                      ))

        self.is_plot_mixshare = attrsman.add(cm.AttrConf('is_plot_mixshare', kwargs.get('is_plot_mixshare', False),
                                                         groupnames=['options'],
                                                         name='Plot mixed share',
                                                         info='Plot  cumulative distribution of share of mixed access roads of shortest route and matched route.',
                                                         ))

        self.is_plot_exclusiveshare = attrsman.add(cm.AttrConf('is_plot_exclusiveshare', kwargs.get('is_plot_exclusiveshare', False),
                                                               groupnames=['options'],
                                                               name='Plot exclusive share',
                                                               info='Plot  cumulative distribution of share of exclusive access roads of shortest route and matched route.',
                                                               ))

        self.is_plot_lowpriorityshare = attrsman.add(cm.AttrConf('is_plot_lowpriorityshare', kwargs.get('is_plot_lowpriorityshare', False),
                                                                 groupnames=['options'],
                                                                 name='Plot low priority share',
                                                                 info='Plot  cumulative distribution of share of low priority roads of shortest route and matched route.',
                                                                 ))

        self.is_plot_nodesdensity = attrsman.add(cm.AttrConf('is_plot_nodesdensity', kwargs.get('is_plot_nodesdensity', False),
                                                             groupnames=['options'],
                                                             name='Plot node ratio',
                                                             info='Plot  cumulative distribution of node ratio between shortest route and matched route.',
                                                             ))

        self.is_plot_tldensity = attrsman.add(cm.AttrConf('is_plot_tldensity', kwargs.get('is_plot_tldensity', False),
                                                          groupnames=['options'],
                                                          name='Plot TL ratio',
                                                          info='Plot  cumulative distribution of traffic light ratio between shortest route and matched route.',
                                                          ))
        self.is_prioritychangedensity = attrsman.add(cm.AttrConf('is_prioritychangedensity', kwargs.get('is_prioritychangedensity', False),
                                                                 groupnames=['options'],
                                                                 name='Plot prio. change dens.',
                                                                 info='Plot  cumulative distribution of priority change denities between shortest route and matched route.',
                                                                 ))

        # comprison  non-overlapping matched and shortest
        self.is_plot_lengthratio_nonoverlap = attrsman.add(cm.AttrConf('is_plot_lengthratio_nonoverlap', kwargs.get('is_plot_lengthratio_nonoverlap', False),
                                                                       groupnames=['options'],
                                                                       name='Plot length ratio non-overlap',
                                                                       info='Plot cumulative distribution  on length ratio between non-overlapping parts of  shortest route and matched route.',
                                                                       ))

        self.is_plot_mixshare_nonoverlap = attrsman.add(cm.AttrConf('is_plot_mixshare_nonoverlap', kwargs.get('is_plot_mixshare_nonoverlap', False),
                                                                    groupnames=['options'],
                                                                    name='Plot mixed share non-overlap',
                                                                    info='Plot  cumulative distribution of share of mixed access roads of non-overlapping parts of shortest route and matched route.',
                                                                    ))

        self.is_plot_exclusiveshare_nonoverlap = attrsman.add(cm.AttrConf('is_plot_exclusiveshare_nonoverlap', kwargs.get('is_plot_exclusiveshare_nonoverlap', False),
                                                                          groupnames=['options'],
                                                                          name='Plot exclusive share non-overlap',
                                                                          info='Plot  cumulative distribution of share of exclusive access roads of non-overlapping parts of shortest route and matched route.',
                                                                          ))

        self.is_plot_lowpriorityshare_nonoverlap = attrsman.add(cm.AttrConf('is_plot_lowpriorityshare_nonoverlap', kwargs.get('is_plot_lowpriorityshare_nonoverlap', False),
                                                                            groupnames=['options'],
                                                                            name='Plot low priority share non-overlap',
                                                                            info='Plot  cumulative distribution of share of low priority roads of non-overlapping parts of shortest route and matched route.',
                                                                            ))

        self.is_plot_nodesdensity_nonoverlap = attrsman.add(cm.AttrConf('is_plot_nodesdensity_nonoverlap', kwargs.get('is_plot_nodesdensity_nonoverlap', False),
                                                                        groupnames=['options'],
                                                                        name='Plot node ratio non-overlap',
                                                                        info='Plot  cumulative distribution of node ratio between non-overlapping parts of shortest route and matched route.',
                                                                        ))

        self.is_plot_tldensity_nonoverlap = attrsman.add(cm.AttrConf('is_plot_tldensity_nonoverlap', kwargs.get('is_plot_tldensity_nonoverlap', False),
                                                                     groupnames=['options'],
                                                                     name='Plot TL ratio non-overlap',
                                                                     info='Plot  cumulative distribution of traffic light ratio between non-overlapping parts of shortest route and matched route.',
                                                                     ))

        self.is_prioritychangedensity_nonoverlap = attrsman.add(cm.AttrConf('is_prioritychangedensity_nonoverlap', kwargs.get('is_prioritychangedensity_nonoverlap', False),
                                                                            groupnames=['options'],
                                                                            name='Plot prio. change dens. non-overlap',
                                                                            info='Plot  cumulative distribution of priority change denities between non-overlapping parts of shortest route and matched route.',
                                                                            ))

        # other
        self.n_bins = attrsman.add(cm.AttrConf('n_bins', kwargs.get('n_bins', 10),
                                               groupnames=['options'],
                                               name='Bin number',
                                               info='Number of bins for histograms.',
                                               ))

        # self.add_plotoptions(**kwargs)
        self.is_title = attrsman.add(cm.AttrConf('is_title', kwargs.get('is_title', False),
                                                 groupnames=['options'],
                                                 name='Show title',
                                                 info='Show title of diagrams.',
                                                 ))

        self.title = attrsman.add(cm.AttrConf('title', kwargs.get('title', ''),
                                              groupnames=['options'],
                                              name='Title',
                                              info='Title text, if blank then default values are used. Only in combination with show title option.',
                                              ))

        self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont', 32),
                                                       groupnames=['options'],
                                                       name='Title fontsize',
                                                       info='Title fontsize.',
                                                       ))

        self.size_labelfont = attrsman.add(cm.AttrConf('size_labelfont', kwargs.get('size_labelfont', 24),
                                                       groupnames=['options'],
                                                       name='Label fontsize',
                                                       info='Label fontsize.',
                                                       ))

        self.width_line = attrsman.add(cm.AttrConf('width_line', kwargs.get('width_line', 2),
                                                   groupnames=['options'],
                                                   perm='wr',
                                                   name='Line width',
                                                   info='Width of plotted lines.',
                                                   ))

        self.color_line = attrsman.add(cm.AttrConf('color_line', kwargs.get('color_line', np.array([0, 0, 0, 1], dtype=np.float32)),
                                                   groupnames=['options'],
                                                   perm='wr',
                                                   metatype='color',
                                                   name='Line color',
                                                   info='Color of line in various diagrams.',
                                                   ))

        # COLOR_MATCHED_ROUTE,COLOR_SHORTEST_ROUTE,COLOR_FASTEST_ROUTE
        self.color_matched = attrsman.add(cm.AttrConf('color_matched', kwargs.get('color_matched', COLOR_MATCHED_ROUTE.copy()),
                                                      groupnames=['options'],
                                                      perm='wr',
                                                      metatype='color',
                                                      name='Color matched data',
                                                      info='Color of matched data in various diagrams.',
                                                      ))

        self.color_shortest = attrsman.add(cm.AttrConf('color_shortest', kwargs.get('color_shortest', COLOR_SHORTEST_ROUTE.copy()),
                                                       groupnames=['options'],
                                                       perm='wr',
                                                       metatype='color',
                                                       name='Color shortest route data',
                                                       info='Color of shortest route data in various diagrams.',
                                                       ))

        self.color_fastest = attrsman.add(cm.AttrConf('color_fastest', kwargs.get('color_fastest', COLOR_FASTEST_ROUTE.copy()),
                                                      groupnames=['options'],
                                                      perm='wr',
                                                      metatype='color',
                                                      name='Color fastest route data',
                                                      info='Color of fastest route data in various diagrams.',
                                                      ))

        self.printformat = attrsman.add(cm.AttrConf('printformat', kwargs.get('printformat', '%.1f'),
                                                    choices=OrderedDict([
                                                        ('Show no values', ''),
                                                        ('x', '%.d'),
                                                        ('x.x', '%.1f'),
                                                        ('x.xx', '%.2f'),
                                                        ('x.xxx', '%.3f'),
                                                        ('x.xxxx', '%.4f'),
                                                    ]),
                                                    groupnames=['options'],
                                                    name='Label formatting',
                                                    info='Print formatting of value label in graphical representation.',
                                                    ))

        self.color_label = attrsman.add(cm.AttrConf('color_label', kwargs.get('color_label', np.array([0, 0, 0, 1], dtype=np.float32)),
                                                    groupnames=['options'],
                                                    perm='wr',
                                                    metatype='color',
                                                    name='Label color',
                                                    info='Color of value label in graphical representation.',
                                                    ))

        self.is_grid = attrsman.add(cm.AttrConf('is_grid', kwargs.get('is_grid', True),
                                                groupnames=['options'],
                                                name='Show grid?',
                                                info='If True, shows a grid on the graphical representation.',
                                                ))
        self.color_background = attrsman.add(cm.AttrConf('color_background', kwargs.get('color_background', np.array([1, 1, 1, 1], dtype=np.float32)),
                                                         groupnames=['options'],
                                                         perm='wr',
                                                         metatype='color',
                                                         name='Background color',
                                                         info='Background color of schematic network in the background.',
                                                         ))

        self.add_save_options(**kwargs)

    def show(self):
        # print 'show',self.edgeattrname
        # if self.axis  is None:
        self.init_figures()
        plt.rc('lines', linewidth=self.width_line)
        # plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
        #                    cycler('linestyle', ['-', '--', ':', '-.'])))
        if self.is_plot_lengthratio:
            self.plot_lengthratio()

        if self.is_plot_lengthoverlap:
            self.plot_lengthoverlap()

        if self.is_plot_lengthoverlap_fastest:
            self.plot_lengthoverlap_fastest()

        if self.is_plot_lengthdistrib:
            self.plot_lengthdistrib()

        if self.is_plot_lengthprob:
            self.plot_lengthprob()

        if self.is_plot_lengthdistrib_by_class:
            self.plot_lengthdistrib_by_class()

        # --
        if self.is_plot_lengthdistrib:
            self.plot_lengthdistrib()

        if self.is_plot_mixshare:
            self.plot_mixshare()

        if self.is_plot_exclusiveshare:
            self.plot_exclusiveshare()

        if self.is_plot_lowpriorityshare:
            self.plot_lowpriorityshare()

        if self.is_plot_nodesdensity:
            self.plot_nodesdensity()

        if self.is_plot_tldensity:
            self.plot_tldensity()

        if self.is_prioritychangedensity:
            self.plot_prioritychangedensity()

        # non overlapping
        if self.is_plot_lengthratio_nonoverlap:
            self.plot_lengthratio_nonoverlap()

        if self.is_plot_mixshare_nonoverlap:
            self.plot_mixshare_nonoverlap()

        if self.is_plot_exclusiveshare_nonoverlap:
            self.plot_exclusiveshare_nonoverlap()

        if self.is_plot_lowpriorityshare_nonoverlap:
            self.plot_lowpriorityshare_nonoverlap()

        if self.is_plot_nodesdensity_nonoverlap:
            self.plot_nodesdensity_nonoverlap()

        if self.is_plot_tldensity_nonoverlap:
            self.plot_tldensity_nonoverlap()

        if self.is_prioritychangedensity_nonoverlap:
            self.plot_prioritychangedensity_nonoverlap()

        if self.is_plot_deptimedistrib:
            self.plot_deptimedistrib()

        if self.is_plot_bike_availability_ffbss:
            self.plot_bike_availability_ffbss()

        if self.is_plot_bike_availability_ffbss_zone:
            self.plot_bike_availability_ffbss_zone()

        if self.is_analyze_bike_availability_ffbss_zones:
            self.analyze_bike_availability_ffbss_zones()

        if not self.is_save:
            show_plot()

    def plot_bike_availability_ffbss(self):
        print('plot_bike_availability_ffbss')
        # Print the number of bikes simultaneously used every 24h/n_bins hours by selecting traces of a particular day
        fig = self.create_figure()
        mapmatching = self.parent.parent
        trips = mapmatching.trips
        ids_trip = trips.get_ids()[(trips.are_selected[trips.get_ids()] == True)]
        dep_arr_times = np.zeros((len(ids_trip), 2))
        for i, id_trip in zip(range(len(ids_trip)), ids_trip):
            t = time.localtime(trips.timestamps[id_trip])
            dep_arr_times[i, 0] = t.tm_hour + t.tm_min/60.0 + t.tm_sec/3600.0
            dep_arr_times[i, 1] = dep_arr_times[i, 0] + trips.durations_gps[id_trip]/3600.0
        self.n_bins
##        dep_arr_times = np.sort(dep_arr_times.sort, axis = 0)
        print(dep_arr_times[:1000])
        ax = fig.add_subplot(111)
        x_min = min(dep_arr_times[:, 0])
        x_max = max(dep_arr_times[:, 0])
        bins = np.linspace(x_min, x_max, self.n_bins)
        bike_usage = np.zeros(self.n_bins)

        for id_trip, dep_arr_time in zip(ids_trip, dep_arr_times):
            for bin, i in zip(bins, range(self.n_bins)):
                if dep_arr_time[0] < bin and dep_arr_time[1] > bin:
                    bike_usage[i-1] += 1
        print(bike_usage)
        bincenters = plt.plot(bins, bike_usage,  color=self.color_matched,
                              label='Bike usage distribution of GPS Mobike traces')
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Bike usage distribution of GPS Mobike traces', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Bike usage', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('bike_usage')

    def plot_bike_availability_ffbss_zone(self):
        print('plot_bike_availability_ffbss_zone')
        # Plot the difference between attracted and generated trips froma zone in a day every 24h/n_bins hours
        fig = self.create_figure()
        mapmatching = self.parent.parent
        trips = mapmatching.trips
        points = mapmatching.points
        ids_trip = trips.get_ids()
        ids_trip = ids_trip[(trips.are_selected[ids_trip] == True) & (trips.ids_points[ids_trip] != int)]
        ids_points = trips.ids_points[ids_trip]
        zone = self.zone_ffbss
        scenario = mapmatching.get_scenario()
        zones = scenario.landuse.zones
        zone_shape = zones.shapes[zones.ids_sumo.get_id_from_index(self.zone_ffbss)]
        n_bins = self.n_bins
        generated_trips = np.zeros((n_bins))
        attracted_trips = np.zeros((n_bins))

        for id_trip, ids_point in zip(ids_trip, ids_points):
            id_initial_point = ids_point[0]
            id_final_point = ids_point[-1]
            print(id_trip)
            starting_time = np.int(points.timestamps[id_initial_point] % 86400/86400.*np.float(n_bins))
            arriving_time = np.int(points.timestamps[id_final_point] % 86400/86400.*np.float(n_bins))

            if is_point_in_polygon(mapmatching.points.coords[id_initial_point], zone_shape):
                generated_trips[starting_time] += 1

            if is_point_in_polygon(mapmatching.points.coords[id_final_point], zone_shape):
                attracted_trips[arriving_time] += 1

        availability = np.cumsum(attracted_trips) - np.cumsum(generated_trips)
        bins = np.linspace(0, 24, num=n_bins)
        np.savetxt('generated_trips_%s.txt' % (zone), generated_trips)
        np.savetxt('attracted_trips_%s.txt' % (zone), attracted_trips)

        ax = fig.add_subplot(131)
        bincenters = plt.plot(bins, availability,  color=self.color_matched,
                              label='Bikes availability')
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Bikes relativeavailability during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Bike Relative Availability', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ax = fig.add_subplot(132)
        bincenters = plt.plot(bins, generated_trips,  color=self.color_matched,
                              label='Generated bikes')
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Generated bikes during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Generated bikes', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ax = fig.add_subplot(133)
        bincenters = plt.plot(bins, attracted_trips,  color=self.color_matched,
                              label='Attracted bikes')
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Attracted bikes during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Attracted bikes', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('bike_availability')

    def analyze_bike_availability_ffbss_zones(self):
        print('analyze_bike_availability_ffbss_zones')
        # Save a .txt file with generated trips and attracted trips every 24h/n_bins hours for each zone with selected trips
        fig = self.create_figure()
        mapmatching = self.parent.parent
        trips = mapmatching.trips
        points = mapmatching.points
        ids_trip = trips.get_ids()
        ids_trip = ids_trip[(trips.are_selected[ids_trip] == True) & (trips.ids_points[ids_trip] != int)]
        ids_points = trips.ids_points[ids_trip]
        zone = self.zone_ffbss
        scenario = mapmatching.get_scenario()
        zones = scenario.landuse.zones
        n_zone = len(zones.get_ids())
        n_bins = self.n_bins
        generated_trips = np.zeros((n_zone, n_bins))
        attracted_trips = np.zeros((n_zone, n_bins))
        for id_trip, ids_point in zip(ids_trip, ids_points):
            id_initial_point = ids_point[0]
            id_final_point = ids_point[-1]
            print(id_trip)
            starting_time = np.int(points.timestamps[id_initial_point] % 86400/86400.*np.float(n_bins))
            arriving_time = np.int(points.timestamps[id_final_point] % 86400/86400.*np.float(n_bins))
            i = 0
            for zone in zones.get_ids():
                zone_shape = zones.shapes[zone]
                if is_point_in_polygon(mapmatching.points.coords[id_initial_point], zone_shape):
                    generated_trips[i, starting_time] += 1
                    break
                i += 1
            i = 0
            for zone in zones.get_ids():
                zone_shape = zones.shapes[zone]
                if is_point_in_polygon(mapmatching.points.coords[id_final_point], zone_shape):
                    attracted_trips[i, arriving_time] += 1
                    break
                i += 1
        np.savetxt('generated_trips.txt', generated_trips)
        np.savetxt('attracted_trips.txt', attracted_trips)

        availability = attracted_trips - generated_trips
        for row in range(len(availability[:, 0])):
            availability[row, :] = np.cumsum(availability[row, :])

        bins = np.linspace(0, 24, num=n_bins)
        np.savetxt('generated_trips_%s.txt' % (zone), generated_trips)
        np.savetxt('attracted_trips_%s.txt' % (zone), attracted_trips)

        ax = fig.add_subplot(231)
        for row in range(len(availability[:, 0])):
            bincenters = plt.plot(bins, availability[row, :],  color=self.color_matched)
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Bikes relativeavailability during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Bike Relative Availability', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ax = fig.add_subplot(232)
        for row in range(len(generated_trips[:, 0])):
            bincenters = plt.plot(bins, generated_trips[row, :],  color=self.color_matched)
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Generated bikes during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Generated bikes', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ax = fig.add_subplot(233)
        for row in range(len(attracted_trips[:, 0])):
            bincenters = plt.plot(bins, attracted_trips[row, :],  color=self.color_matched)
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Attracted bikes during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Attracted bikes', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('bike_availability')

        # plot relative values
        availability2 = np.zeros((len(availability[:, 0]), len(availability[0, :])))
        generated_trips2 = np.zeros((len(availability[:, 0]), len(availability[0, :])))
        attracted_trips2 = np.zeros((len(availability[:, 0]), len(availability[0, :])))

        for row in range(len(availability[:, 0])):
            availability2[row, :] = availability[row, :]/np.sum(np.absolute(availability[row, :]))
            generated_trips2[row, :] = generated_trips[row, :]/np.sum(generated_trips[row, :])
            attracted_trips2[row, :] = attracted_trips[row, :]/np.sum(attracted_trips[row, :])

        ax = fig.add_subplot(234)
        for row in range(len(availability[:, 0])):
            if np.sum(generated_trips[row, :]) > n_bins and np.sum(attracted_trips[row, :]) > n_bins:
                bincenters = plt.plot(bins, availability2[row, :],  color=self.color_matched)
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Bikes relativeavailability during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Bike Relative Availability', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ax = fig.add_subplot(235)
        for row in range(len(generated_trips[:, 0])):
            if np.sum(generated_trips[row, :]) > n_bins and np.sum(attracted_trips[row, :]) > n_bins:
                bincenters = plt.plot(bins, generated_trips2[row, :],  color=self.color_matched)
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Generated bikes during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Generated bikes', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        ax = fig.add_subplot(236)
        for row in range(len(attracted_trips[:, 0])):
            if np.sum(generated_trips[row, :]) > n_bins and np.sum(attracted_trips[row, :]) > n_bins:
                bincenters = plt.plot(bins, attracted_trips2[row, :],  color=self.color_matched)
        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Attracted bikes during time', fontsize=self.size_titlefont)
        ax.set_xlabel('time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Attracted bikes', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('bike_availability')

    def plot_deptimedistrib(self):
        print('plot_deptimedistrib')
        fig = self.create_figure()
        mapmatching = self.parent.parent
        trips = mapmatching.trips
        ids_trip = trips.get_ids()
        hour_departure = np.zeros(len(ids_trip))
        for i, id_trip in zip(range(len(ids_trip)), ids_trip):
            t = time.localtime(trips.timestamps[id_trip])
            hour_departure[i] = t.tm_hour + t.tm_min/60.0 + t.tm_sec/3600.0
        # print hour_departure[:1000]

        ax = fig.add_subplot(111)
        x_min = min(hour_departure)
        x_max = max(hour_departure)
        if self.n_bins < 0:
            bins = np.arange(x_min, x_max + 1, 1)

        else:
            n_bins = self.n_bins
            bins = np.linspace(x_min, x_max, n_bins)

        # print '  bins',bins
        bincenters = plt.hist(hour_departure, bins=bins, color=self.color_line,
                              density=True,
                              histtype='stepfilled',
                              label='Departure time (hour) distribution')

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            if self.title == "":
                title = 'Departure time distribution of GPS traces'
            else:
                title = self.title
            ax.set_title(title, fontsize=self.size_titlefont)
        ax.set_xlabel('Departure time [h]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('plot_deptimedistrib')

    def plot_tldensity(self):
        print('plot_tldensity')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        matched = routesresults_matched.numbers_nodes_tls[ids_valid]/dists_match*1000
        shortest = routesresults_shortest.numbers_nodes_tls[ids_valid]/dists_shortest*1000

        x_min = min(np.min(matched), np.min(shortest))
        x_max = 10.0  # max(np.max(matched),np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Node densities of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Traffic light density [1/km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_tldensity')

    def plot_nodesdensity(self):
        print('plot_nodesdensity')
        fig = self.create_figure()
        results = self.parent

        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        matched = routesresults_matched.numbers_nodes[ids_valid]/dists_match*1000
        shortest = routesresults_shortest.numbers_nodes[ids_valid]/dists_shortest*1000

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Node densities of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Node density [1/km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_nodesdensity')

    def plot_prioritychangedensity(self):
        print('plot_prioritychangedensity')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        matched = routesresults_matched.numbers_prioritychange[ids_valid]/dists_match*1000
        shortest = routesresults_shortest.numbers_prioritychange[ids_valid]/dists_shortest*1000

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Priority change dens. of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Priority change density [1/km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_nodesdensity')

    def plot_lowpriorityshare(self):
        print('plot_lowpriorityshare')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        matched = routesresults_matched.lengths_low_priority[ids_valid]/dists_match*100
        shortest = routesresults_shortest.lengths_low_priority[ids_valid]/dists_shortest*100

        x_min = min(np.min(matched), np.min(shortest))
        x_max = 15.0  # max(np.max(matched),np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of low priority roads of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Low priority road share [%]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_lowpriorityshare')

    def plot_exclusiveshare(self):
        print('plot_exclusiveshare')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        matched = routesresults_matched.lengths_exclusive[ids_valid]/dists_match*100
        shortest = routesresults_shortest.lengths_exclusive[ids_valid]/dists_shortest*100

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of exclusive access roads of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Exclusive access road share [%]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_exclusiveshare')

    def plot_mixshare(self):
        print('plot_mixshare')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        matched = routesresults_matched.lengths_mixed[ids_valid]/dists_match*100
        shortest = routesresults_shortest.lengths_mixed[ids_valid]/dists_shortest*100

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of mixed reserved access roads of matched and shortest route',
                         fontsize=self.size_titlefont)
        ax.set_xlabel('Mixed reserved access road share [%]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_mixshare')

    def plot_lengthdistrib_by_class(self):
        print('plot_lengthdistrib_by_class')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]
        x_min = min(np.min(dists_match), np.min(dists_shortest))
        x_max = max(np.max(dists_match), np.max(dists_shortest))

        dists_class = np.arange(0, int(x_max), self.distance_class)
        dists_class_center = (dists_class+0.5*self.distance_class)[1:]
        n_class = len(dists_class)-1
        means_match = np.zeros(n_class, dtype=np.float32)
        stds_match = np.zeros(n_class, dtype=np.float32)
        means_shortest = np.zeros(n_class, dtype=np.float32)
        stds_shortest = np.zeros(n_class, dtype=np.float32)
        xticklabels = []
        ratiolabels = []
        for dist_lower, dist_upper, i in zip(dists_class[:-1], dists_class[1:], range(n_class)):
            xticklabels.append('%d - %d' % (float(dist_lower)/1000, float(dist_upper)/1000))
            inds = np.logical_and(dists_match > dist_lower, dists_match < dist_upper)
            means_match[i] = np.mean(dists_match[inds])
            stds_match[i] = np.std(dists_match[inds])

            #inds = np.logical_and(dists_shortest>dist_lower,dists_shortest<dist_upper)
            means_shortest[i] = np.mean(dists_shortest[inds])
            stds_shortest[i] = np.std(dists_shortest[inds])

            ratiolabel = ''
            if (not np.isnan(means_shortest[i])) & (not np.isnan(means_match[i])):
                if means_match[i] > 0:
                    ratiolabel = '%d%%' % (means_shortest[i]/means_match[i]*100)
            ratiolabels.append(ratiolabel)

        print('  dists_class_center', dists_class_center)
        print('  means_match', means_match)
        print('  stds_match', stds_match)
        print('  means_shortest', means_shortest)
        print('  stds_shortest', stds_shortest)

        x = np.arange(n_class, dtype=np.float32)  # the x locations for the groups
        width = 0.35       # the width of the bars

        #        ax.bar(ind + width, women_means, width, color='y', yerr=women_std)
        bars1 = ax.bar(x-width, means_match, width, color=self.color_matched, yerr=stds_match)
        bars2 = ax.bar(x+0*width, means_shortest, width, color=self.color_shortest, yerr=stds_shortest)
        #bars1 = ax.bar(dists_class_center+0.35*self.distance_class, means_match, 0.25*self.distance_class, color=self.color_matched, yerr=stds_match)
        #bars2 = ax.bar(dists_class_center-0.35*self.distance_class, means_shortest, 0.25*self.distance_class, color=self.color_shortest, yerr=stds_shortest)

        #ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)
        ax.legend((bars1[0], bars2[0]), ('matched', 'shortest'),
                  shadow=True, fontsize=self.size_labelfont, loc='best')

        # if self.is_grid:
        ax.yaxis.grid(self.is_grid)

        if self.is_title:
            ax.set_title('Mean length by trip length class', fontsize=self.size_titlefont)
        ax.set_xlabel('Length classes [km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Mean length [m]', fontsize=self.size_labelfont)

        ax.set_xticks(x)
        ax.set_xticklabels(xticklabels)
        # self._autolabel_bars(ax,bars1,means_match)
        self._autolabel_bars(ax, bars2, ratiolabels)

        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_lengthdistrib_by_class')

    def _autolabel_bars(self, ax, bars, labels):
        """
        Attach a text label above each bar displaying its height
        """
        for rect, label in zip(bars, labels):
            height = rect.get_height()
            if not np.isnan(height):
                ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                        '%s' % label,
                        ha='center', va='bottom',
                        fontsize=int(0.8*self.size_labelfont),
                        )

    def plot_lengthdistrib(self):

        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(routesresults_shortest.distances.get_value() > 0,
                                                                    time.                                          routesresults_matched.distances.get_value() > 0,
                                                                    # routesresults_matched.distances.get_value()<20000)
                                                                    ))
        print('plot_lengthdistrib', len(ids_valid))
        # print '  ids_valid',ids_valid
        if len(ids_valid) == 0:
            return False

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        x_min = min(np.min(dists_match), np.min(dists_shortest))
        x_max = max(np.max(dists_match), np.max(dists_shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, dists_match, bins=bins, color=self.color_matched,
                                    label='matched:'+'$\mu = %dm$, $\sigma=%dm$' % (np.mean(dists_match), np.std(dists_match)))
        bincenters = self.plot_hist(ax, dists_shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %dm$, $\sigma=%dm$' % (np.mean(dists_shortest), np.std(dists_shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Length distribution of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Length [m]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_lengthdistrib')
        return True

    def plot_timedistrib(self):
        print('plot_timedistrib')
        fig = self.create_figure()
        results = self.parent
        mapmatching = results.parent
        trips = mapmatching.trips
        #routesresults_fastest = results.routesresults_fastest
        #routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        #ids_overlength = routesresults_matched.select_ids(np.logical_and(routesresults_shortest.distances.get_value()>0, routesresults_matched.distances.get_value()>20000))
        # print '  len(ids_overlength)',len(ids_overlength)
        # print '  ids_overlength',ids_overlength

        ids_valid = trips.select_ids(
            np.logical_and(trips.lengths_route_matched.get_value() > 0,
                           trips.durations_route_fastest.get_value() > 0,
                           # routesresults_matched.distances.get_value()<20000)
                           ))

        if len(ids_valid) == 0:
            return False

        times_fastest = trips.durations_route_fastest[ids_valid]
        times_match = times_fastest+trips.timelosses_route_fastest[ids_valid]

        x_min = min(np.min(times_fastest), np.min(times_match))
        x_max = max(np.max(times_fastest), np.max(times_match))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, times_match, bins=bins, color=self.color_matched,
                                    label='matched:'+'$\mu = %dm$, $\sigma=%dm$' % (np.mean(times_match), np.std(times_match)))
        bincenters = self.plot_hist(ax, times_fastest, bins=bins, color=self.color_fastest, label='fastest:' +
                                    '$\mu = %dm$, $\sigma=%dm$' % (np.mean(times_fastest), np.std(times_fastest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Triptime distribution of matched and fastest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Time [s]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_timedistrib')

        return True

    def plot_lengthprob(self):
        print('plot_lengthprob')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))

        dists_match = routesresults_matched.distances[ids_valid]
        dists_shortest = routesresults_shortest.distances[ids_valid]

        x_min = min(np.min(dists_match), np.min(dists_shortest))
        x_max = max(np.max(dists_match), np.max(dists_shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        w_bin = bins[1]-bins[0]
        bincenters = self.plot_hist(ax, dists_match, bins=bins,
                                    color=self.color_matched,
                                    label='matched:' +
                                    '$\mu = %dm$, $\sigma=%dm$' % (np.mean(dists_match), np.std(dists_match)),
                                    is_rel_frequ=True,
                                    is_percent=True,
                                    )
        bincenters = self.plot_hist(ax, dists_shortest, bins=bins,
                                    color=self.color_shortest,
                                    label='shortest:' +
                                    '$\mu = %dm$, $\sigma=%dm$' % (np.mean(dists_shortest), np.std(dists_shortest)),
                                    is_rel_frequ=True,
                                    is_percent=True,
                                    )

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Relative frequency of matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Length [m]', fontsize=self.size_labelfont)
        ax.set_ylabel('Relative frequency [%]', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_lengthprob')

    def plot_lengthoverlap(self):
        print('plot_lengthoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        bins = np.linspace(0.0, 1.0, self.n_bins)

        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))
        values = routesresults_shortest.lengths_overlap_matched[ids_valid]/routesresults_matched.distances[ids_valid]
        bincenters = self.plot_hist(ax, values,
                                    bins=bins, histtype='bar',
                                    label=r'$\mu = %.2f$, $\sigma=%.2f$' % (np.mean(values), np.std(values))
                                    )

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of overlap between shortest path and matched path', fontsize=self.size_titlefont)
        ax.set_xlabel('Overlap share between shortest and matched path', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        self.set_figmargins()
        if self.is_save:
            self.save_fig('routeana_lengthoverlap')

    def plot_lengthoverlap_fastest(self):
        print('plot_lengthoverlap_fastest')
        fig = self.create_figure()
        results = self.parent
        print('dir(results)', dir(results))
        routesresults_fastest = results.routesresults_fastest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        bins = np.linspace(0.0, 1.0, self.n_bins)

        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_fastest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))
        values = routesresults_fastest.lengths_overlap_matched[ids_valid]/routesresults_matched.distances[ids_valid]
        bincenters = self.plot_hist(ax, values,
                                    bins=bins, histtype='bar',
                                    label=r'$\mu = %.2f$, $\sigma=%.2f$' % (np.mean(values), np.std(values))
                                    )

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of overlap between fastest path and matched path', fontsize=self.size_titlefont)
        ax.set_xlabel('Overlap share between fastest and matched path', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        self.set_figmargins()
        if self.is_save:
            self.save_fig('routeana_lengthoverlap_fastest')

    def plot_lengthratio(self):
        print('plot_lengthratio')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        bins = np.linspace(0.0, 1.0, self.n_bins)

        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest.distances.get_value() > 0, routesresults_matched.distances.get_value() > 0))
        values = routesresults_shortest.distances[ids_valid]/routesresults_matched.distances[ids_valid]
        bincenters = self.plot_hist(ax, values,
                                    bins=bins, histtype='bar',
                                    label=r'$\mu = %.2f$, $\sigma=%.2f$' % (np.mean(values), np.std(values))
                                    )

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Ratio distance shortest path over matched path', fontsize=self.size_titlefont)
        ax.set_xlabel('Ratio shortest path length/matched path length', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        self.set_figmargins()
        if self.is_save:
            self.save_fig('routeana_lengthratio')


# -------------------------------------------------------------------------------
    # non-overlap

    def plot_lengthratio_nonoverlap(self):
        print('plot_lengthratio_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap

        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        bins = np.linspace(0.0, 1.0, self.n_bins)

        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0,
            routesresults_matched_nonoverlap.distances.get_value() > 0)
        )

        values = routesresults_shortest_nonoverlap.distances[ids_valid] / \
            routesresults_matched_nonoverlap.distances[ids_valid]
        bincenters = self.plot_hist(ax, values,
                                    bins=bins, histtype='bar',
                                    label=r'$\mu = %.2f$, $\sigma=%.2f$' % (np.mean(values), np.std(values))
                                    )

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Ratio distance of non-overlapping shortest over matched path', fontsize=self.size_titlefont)
        ax.set_xlabel('Ratio shortest n.o. path length/matched path length', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))

        self.set_figmargins()
        if self.is_save:
            self.save_fig('routeana_lengthratio_nonoverlap')

    def plot_tldensity_nonoverlap(self):
        print('plot_tldensity_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0, routesresults_matched_nonoverlap.distances.get_value() > 0))

        dists_match = routesresults_matched_nonoverlap.distances[ids_valid]
        dists_shortest = routesresults_shortest_nonoverlap.distances[ids_valid]

        matched = routesresults_matched_nonoverlap.numbers_nodes_tls[ids_valid]/dists_match*1000
        shortest = routesresults_shortest_nonoverlap.numbers_nodes_tls[ids_valid]/dists_shortest*1000

        x_min = min(np.min(matched), np.min(shortest))
        x_max = 10.0  # max(np.max(matched),np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Node densities of non-overlapping matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Traffic light density  n.o. [1/km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_tldensity_nonoverlap')

    def plot_nodesdensity_nonoverlap(self):
        print('plot_nodesdensity_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0, routesresults_matched_nonoverlap.distances.get_value() > 0))

        dists_match = routesresults_matched_nonoverlap.distances[ids_valid]
        dists_shortest = routesresults_shortest_nonoverlap.distances[ids_valid]

        matched = routesresults_matched_nonoverlap.numbers_nodes[ids_valid]/dists_match*1000
        shortest = routesresults_shortest_nonoverlap.numbers_nodes[ids_valid]/dists_shortest*1000

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Node densities of non-overlapping matched and shortest route', fontsize=self.size_titlefont)
        ax.set_xlabel('Node density  n.o. [1/km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_nodesdensity_nonoverlap')

    def plot_prioritychangedensity_nonoverlap(self):
        print('plot_prioritychangedensity_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0, routesresults_matched_nonoverlap.distances.get_value() > 0))

        dists_match = routesresults_matched_nonoverlap.distances[ids_valid]
        dists_shortest = routesresults_shortest_nonoverlap.distances[ids_valid]

        matched = routesresults_matched_nonoverlap.numbers_prioritychange[ids_valid]/dists_match*1000
        shortest = routesresults_shortest_nonoverlap.numbers_prioritychange[ids_valid]/dists_shortest*1000

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f/km$, $\sigma=%.2f/km$' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Priority change dens. of non-overlapping matched and shortest route',
                         fontsize=self.size_titlefont)
        ax.set_xlabel('Priority change density  n.o. [1/km]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_nodesdensity_nonoverlap')

    def plot_lowpriorityshare_nonoverlap(self):
        print('plot_lowpriorityshare_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0, routesresults_matched_nonoverlap.distances.get_value() > 0))

        dists_match = routesresults_matched_nonoverlap.distances[ids_valid]
        dists_shortest = routesresults_shortest_nonoverlap.distances[ids_valid]

        matched = routesresults_matched_nonoverlap.lengths_low_priority[ids_valid]/dists_match*100
        shortest = routesresults_shortest_nonoverlap.lengths_low_priority[ids_valid]/dists_shortest*100

        x_min = min(np.min(matched), np.min(shortest))
        x_max = 15.0  # max(np.max(matched),np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of low priority roads of non-overlapping matched and shortest route',
                         fontsize=self.size_titlefont)
        ax.set_xlabel('Low priority road share  n.o. [%]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_lowpriorityshare_nonoverlap')

    def plot_exclusiveshare_nonoverlap(self):
        print('plot_exclusiveshare_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0, routesresults_matched_nonoverlap.distances.get_value() > 0))

        dists_match = routesresults_matched_nonoverlap.distances[ids_valid]
        dists_shortest = routesresults_shortest_nonoverlap.distances[ids_valid]

        matched = routesresults_matched_nonoverlap.lengths_exclusive[ids_valid]/dists_match*100
        shortest = routesresults_shortest_nonoverlap.lengths_exclusive[ids_valid]/dists_shortest*100

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of exclusive access roads of non-overlapping matched and shortest route',
                         fontsize=self.size_titlefont)
        ax.set_xlabel('Exclusive access road share  n.o. [%]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_exclusiveshare_nonoverlap')

    def plot_mixshare_nonoverlap(self):
        print('plot_mixshare_nonoverlap')
        fig = self.create_figure()
        results = self.parent
        routesresults_shortest = results.routesresults_shortest
        routesresults_matched = results.routesresults_matched
        routesresults_shortest_nonoverlap = results.routesresults_shortest_nonoverlap
        routesresults_matched_nonoverlap = results.routesresults_matched_nonoverlap
        edgesresults = results.edgesresults

        ax = fig.add_subplot(111)
        ids_valid = routesresults_matched.select_ids(np.logical_and(
            routesresults_shortest_nonoverlap.distances.get_value() > 0, routesresults_matched_nonoverlap.distances.get_value() > 0))

        dists_match = routesresults_matched_nonoverlap.distances[ids_valid]
        dists_shortest = routesresults_shortest_nonoverlap.distances[ids_valid]

        matched = routesresults_matched_nonoverlap.lengths_mixed[ids_valid]/dists_match*100
        shortest = routesresults_shortest_nonoverlap.lengths_mixed[ids_valid]/dists_shortest*100

        x_min = min(np.min(matched), np.min(shortest))
        x_max = max(np.max(matched), np.max(shortest))
        bins = np.linspace(x_min, x_max, self.n_bins)
        bincenters = self.plot_hist(ax, matched, bins=bins, color=self.color_matched, label='matched:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(matched), np.std(matched)))
        bincenters = self.plot_hist(ax, shortest, bins=bins, color=self.color_shortest, label='shortest:' +
                                    '$\mu = %.2f$%%, $\sigma=%.2f$%%' % (np.mean(shortest), np.std(shortest)))

        ax.legend(loc='best', shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Share of mixed reserved access roads of non-overlapping matched and shortest route',
                         fontsize=self.size_titlefont)
        ax.set_xlabel('Mixed reserved access road share n.o. [%]', fontsize=self.size_labelfont)
        ax.set_ylabel('Probability distribution', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        if self.is_save:
            self.save_fig('routeana_mixshare_nonoverlap')
# -------------------------------------------------------------------------------

    def do(self):
        # print 'do',self.edgeattrname
        self.show()

    def get_scenario(self):
        return self.parent.get_scenario()


class PtFlowdigramPlotter(PlotoptionsMixin, Process):
    def __init__(self, results, name='PT flowdiagram',
                 info="Plots the flow diagram of PT lines using matplotlib",
                 logger=None, **kwargs):

        self._init_common('ptrouteresultplotter', parent=results, name=name,
                          info=info, logger=logger)

        print('PtFlowdigramPlotter.__init__', results, self.parent)
        attrsman = self.get_attrsman()

        self.id_line = attrsman.add(cm.AttrConf('id_line', kwargs.get('id_line', -1),
                                                groupnames=['options'],
                                                name='Line ID',
                                                info='ID of public transport line to be plotted.',
                                                ))

        self.is_add_similar = attrsman.add(cm.AttrConf('is_add_similar', kwargs.get('is_add_similar', True),
                                                       groupnames=['options'],
                                                       name='include similar lines',
                                                       info='Add also trips on similar PT lines.',
                                                       ))

        self.is_title = attrsman.add(cm.AttrConf('is_title', kwargs.get('is_title', False),
                                                 groupnames=['options'],
                                                 name='Show title',
                                                 info='Show title of diagrams.',
                                                 ))

        self.size_titlefont = attrsman.add(cm.AttrConf('size_titlefont', kwargs.get('size_titlefont', 32),
                                                       groupnames=['options'],
                                                       name='Title fontsize',
                                                       info='Title fontsize.',
                                                       ))

        self.size_labelfont = attrsman.add(cm.AttrConf('size_labelfont', kwargs.get('size_labelfont', 24),
                                                       groupnames=['options'],
                                                       name='Label fontsize',
                                                       info='Label fontsize.',
                                                       ))

        self.width_line = attrsman.add(cm.AttrConf('width_line', kwargs.get('width_line', 2),
                                                   groupnames=['options'],
                                                   perm='wr',
                                                   name='Line width',
                                                   info='Width of plotted lines.',
                                                   ))

        # self.color_outline = attrsman.add(cm.AttrConf( 'color_outline', kwargs.get('color_outline', np.array([0.0,0.0,0.0,0.95], dtype=np.float32)),
        #                                groupnames = ['options'],
        #                                perm='wr',
        #                                metatype = 'color',
        #                                name = 'Outline color',
        #                                info = 'Outline color of result arrows in graphical representation. Only valid if no color-fill is chosen.',
        #                                ))

        self.color_fill = attrsman.add(cm.AttrConf('color_fill', kwargs.get('color_fill', np.array([0.3, 0.3, 1.0, 0.95], dtype=np.float32)),
                                                   groupnames=['options'],
                                                   perm='wr',
                                                   metatype='color',
                                                   name='Fill color',
                                                   info='Fill color of result arrows in graphical representation.',
                                                   ))

        self.is_grid = attrsman.add(cm.AttrConf('is_grid', kwargs.get('is_grid', False),
                                                groupnames=['options'],
                                                name='Show grid?',
                                                info='If True, shows a grid on the graphical representation.',
                                                ))

        self.csvfilepath = attrsman.add(cm.AttrConf('csvfilepath', kwargs.get('csvfilepath', ''),
                                                    groupnames=['options'],
                                                    perm='rw',
                                                    name='CSV filename',
                                                    wildcards='CSV file (*.csv)|*.csv|All file (*.*)|*.*',
                                                    metatype='filepaths',
                                                    info="""Filename to export flow results.""",
                                                    ))

        self.add_save_options(**kwargs)

    def show(self):
        print('show')
        # if self.axis  is None:
        self.init_figures()
        plt.rc('lines', linewidth=self.width_line)
        fig = self.create_figure()
        ax = fig.add_subplot(111)

        results = self.parent

        ptlinesresults = results.ptlinesresults

        # link object
        mapmatching = results.parent
        #trips = mapmatching.trips
        #routes = trips.get_routes()

        scenario = mapmatching.get_scenario()
        ptstops = scenario.net.ptstops
        demand = scenario.demand
        ptlines = demand.ptlines
        ptlinks = ptlines.get_ptlinks()

        if not self.id_line in ptlines:
            print('WARNING: line with ID', self.id_line, 'not found.')
            return False

        #id_line = ptlines.linenames.get_id_from_index(self.linename)
        if self.is_add_similar:
            linename = ptlines.linenames[self.id_line].split('_')[0]
        else:
            linename = ptlines.linenames[self.id_line]
        ids_stoptuple_line, tripnumbers_line = ptlinesresults.get_flowdiagramm(
            self.id_line, is_add_similar=self.is_add_similar)

        n_stops = len(ids_stoptuple_line)
        ax.bar(xrange(n_stops), tripnumbers_line,
               width=1.0, bottom=0, align='edge',
               color=self.color_fill,
               #linecolor = self.color_outline,
               linewidth=self.width_line,)

        stopnames = []
        for id_fromstop, id_tostop in ids_stoptuple_line:
            stopnames.append(ptstops.stopnames_human[id_fromstop])
        # print  '  stopnames',stopnames
        ax.set_xticks(xrange(n_stops))
        ax.set_xticklabels(stopnames)

        #ax.legend(loc='best',shadow=True, fontsize=self.size_labelfont)
        ax.grid(self.is_grid)
        if self.is_title:
            ax.set_title('Flow diagram of PT line '+linename, fontsize=self.size_titlefont)
        ax.set_xlabel('Stops', fontsize=self.size_labelfont)
        ax.set_ylabel('Number of Passengers', fontsize=self.size_labelfont)
        ax.tick_params(axis='x', labelsize=int(0.8*self.size_labelfont))
        ax.tick_params(axis='y', labelsize=int(0.8*self.size_labelfont))
        plt.setp(ax.get_xticklabels(), rotation=90, ha="right", rotation_mode="anchor")

        if len(self.csvfilepath) > 0:
            # export to csv
            f = open(self.csvfilepath, 'w')
            f.write('Flow diagram of PT line '+linename+'\n\n')
            sep = ','
            f.write('Stopname from'+sep+'Stopname from'+sep+'Passengers\n')
            for ids_stoptuple, number in zip(ids_stoptuple_line, tripnumbers_line):
                f.write(ptstops.stopnames_human[ids_stoptuple[0]]+sep +
                        ptstops.stopnames_human[ids_stoptuple[1]]+sep+str(number)+'\n')

            f.close()

        if self.is_save:
            self.save_fig('_ptrouteana_flowdiagram')
        else:
            show_plot()