summaryrefslogtreecommitdiff
path: root/PyMca5/PyMcaGraph/Plot.py
blob: afd6afb6c726dbcd7d827971c4808ed65c21062f (plain)
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
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2019 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__doc__ = """
This module can be used for plugin testing purposes as well as for doing
the bookkeeping of actual plot windows.

Functions to be implemented by an actual plotter can be found in the
abstract class PlotBackend.

"""
import math
import sys
import numpy
from . import PlotBase
from . import PlotBackend
from . import Colors

import logging
import traceback
_logger = logging.getLogger(__name__)

DEBUG = 0
if DEBUG:
    PlotBase.DEBUG = True
    _logger.setLevel(logging.DEBUG)

_COLORDICT =  Colors.COLORDICT
_COLORLIST = [_COLORDICT['black'],
              _COLORDICT['blue'],
              _COLORDICT['red'],
              _COLORDICT['green'],
              _COLORDICT['pink'],
              _COLORDICT['yellow'],
              _COLORDICT['brown'],
              _COLORDICT['cyan'],
              _COLORDICT['magenta'],
              _COLORDICT['orange'],
              _COLORDICT['violet'],
              #_COLORDICT['bluegreen'],
              _COLORDICT['grey'],
              _COLORDICT['darkBlue'],
              _COLORDICT['darkRed'],
              _COLORDICT['darkGreen'],
              _COLORDICT['darkCyan'],
              _COLORDICT['darkMagenta'],
              _COLORDICT['darkYellow'],
              _COLORDICT['darkBrown']]

#
#Matplotlib symbols:
#"." 	point
#"," 	pixel
#"o" 	circle
#"v" 	triangle_down
#"^" 	triangle_up
#"<" 	triangle_left
#">" 	triangle_right
#"1" 	tri_down
#"2" 	tri_up
#"3" 	tri_left
#"4" 	tri_right
#"8" 	octagon
#"s" 	square
#"p" 	pentagon
#"*" 	star
#"h" 	hexagon1
#"H" 	hexagon2
#"+" 	plus
#"x" 	x
#"D" 	diamond
#"d" 	thin_diamond
#"|" 	vline
#"_" 	hline
#"None" 	nothing
#None 	nothing
#" " 	nothing
#"" 	nothing
#

DEFAULT_BACKEND = "matplotlib"

class Plot(PlotBase.PlotBase):
    PLUGINS_DIR = None
    # give the possibility to set the default backend for all instances
    # via a class attribute.
    defaultBackend = DEFAULT_BACKEND

    colorList = _COLORLIST
    colorDict = _COLORDICT

    def __init__(self, parent=None, backend=None, callback=None):
        self._parent = parent
        if backend is None:
            backend = Plot.defaultBackend
            self._default = True
        else:
            self._default = False
        if hasattr(backend, "__call__"):
            # to be called
            self._plot = backend(parent)
        elif isinstance(backend, PlotBackend.PlotBackend):
            self._plot = backend
        elif hasattr(backend, "lower"):
            lowerCaseString = backend.lower()
            if lowerCaseString in ["matplotlib", "mpl"]:
                from .backends.MatplotlibBackend import MatplotlibBackend as be
            elif lowerCaseString in ["gl", "opengl"]:
                from .backends.OpenGLBackend import OpenGLBackend as be
            elif lowerCaseString in ["glut"]:
                from .backends.GLUTOpenGLBackend import GLUTOpenGLBackend as be
            elif lowerCaseString in ["osmesa", "mesa"]:
                from .backends.OSMesaGLBackend import OSMesaGLBackend as be
            elif lowerCaseString in ["silx", "silx-mpl", "silxmpl",
                                     "silx-gl", "silxgl"]:
                from .backends.SilxBackend import SilxBackend as be
            else:
                raise ValueError("Backend not understood %s" % backend)
            if lowerCaseString in ["silx-gl", "silxgl"]:
                self._plot = be(parent, backend="gl")
            elif lowerCaseString in ["silx-mpl", "silxmpl"]:
                self._plot = be(parent, backend="mpl")
            else:
                self._plot = be(parent)
        super(Plot, self).__init__()
        widget = self._plot.getWidgetHandle()
        if widget is None:
            self.widget_ = self._plot
        else:
            self.widget_ = widget

        self.setCallback(callback)

        self.setLimits = self._plot.setLimits

        # curve handling
        self._curveList = []
        self._curveDict = {}
        self._activeCurve = None
        self._hiddenCurves = []

        #image handling
        self._imageList = []
        self._imageDict = {}
        self._activeImage = None

        # marker handling
        self._markerList = []
        self._markerDict = {}

        # item handling
        self._itemList = []
        self._itemDict = {}

        # line types
        self._styleList = ['-', '--', '-.', ':']
        self._nColors   = len(self.colorList)
        self._nStyles   = len(self._styleList)

        self._colorIndex = 0
        self._styleIndex = 0
        self._activeCurveColor = "#000000"

        # default properties
        self._logY = False
        self._logX = False

        self.setDefaultPlotPoints(False)
        self.setDefaultPlotLines(True)

        # zoom handling (should we take care of it?)
        self.enableZoom = self.setZoomModeEnabled
        self.setZoomModeEnabled(True)

        self._defaultDataMargins = (0., 0., 0., 0.)

    def enableActiveCurveHandling(self, flag=True):
        activeCurve = None
        if not flag:
            if self.isActiveCurveHandlingEnabled():
                activeCurve = self.getActiveCurve()
            self._activeCurveHandling = False
            self._activeCurve = None
        else:
            self._activeCurveHandling = True
        self._plot.enableActiveCurveHandling(self._activeCurveHandling)
        if activeCurve not in [None, []]:
            self.addCurve(activeCurve[0],
                          activeCurve[1],
                          legend=activeCurve[2],
                          info=activeCurve[3])

    def isZoomModeEnabled(self):
        return self._plot.isZoomModeEnabled()

    def isDrawModeEnabled(self):
        return self._plot.isDrawModeEnabled()

    def getWidgetHandle(self):
        return self.widget_

    def setCallback(self, callbackFunction):
        if callbackFunction is None:
            self._plot.setCallback(self.graphCallback)
        else:
            self._plot.setCallback(callbackFunction)

    def graphCallback(self, ddict=None):
        """
        This callback is foing to receive all the events from the plot.
        Those events will consist on a dictionary and among the dictionary
        keys the key 'event' is mandatory to describe the type of event.
        This default implementation only handles setting the active curve.
        """

        if ddict is None:
            ddict = {}
        if DEBUG:
            print("Received dict keys = ", ddict.keys())
            print(ddict)
        if ddict['event'] in ["legendClicked", "curveClicked"]:
            if ddict['button'] == "left":
                self.setActiveCurve(ddict['label'])

    def setDefaultPlotPoints(self, flag):
        if flag:
            self._plotPoints = True
        else:
            self._plotPoints = False
        for key in self._curveList:
            if 'plot_symbol' in self._curveDict[key][3]:
                del self._curveDict[key][3]['plot_symbol']
        if len(self._curveList):
            self._update()

    def setDefaultPlotLines(self, flag):
        if flag:
            self._plotLines = True
        else:
            self._plotLines = False
        if len(self._curveList):
            self._update()

    def _getColorAndStyle(self):
        self._lastColorIndex = self._colorIndex
        self._lastStyleIndex = self._styleIndex
        if self._colorIndex >= self._nColors:
            self._colorIndex = 0
            self._styleIndex += 1
            if self._styleIndex >= self._nStyles:
                self._styleIndex = 0
        color = self.colorList[self._colorIndex]
        style = self._styleList[self._styleIndex]
        if color == self._activeCurveColor:
            self._colorIndex += 1
            if self._colorIndex >= self._nColors:
                self._colorIndex = 0
                self._styleIndex += 1
                if self._styleIndex >= self._nStyles:
                    self._styleIndex = 0
            color = self.colorList[self._colorIndex]
            style = self._styleList[self._styleIndex]
        self._colorIndex += 1
        return color, style

    def setZoomModeEnabled(self, flag=True, color="black"):
        """
        Zoom and drawing are not compatible and cannot be enabled simultanelously

        :param flag: If True, the user can zoom.
        :type flag: boolean, default True
        :param color: The color to use to draw the selection area.
                      Default 'black"
        :param color: The color to use to draw the selection area
        :type color: string ("#RRGGBB") or 4 column unsigned byte array or
                     one of the predefined color names defined in Colors.py
        """
        self._plot.setZoomModeEnabled(flag=flag, color=color)

    def setDrawModeEnabled(self, flag=True, shape="polygon", label=None,
                           color=None, **kw):
        """
        Zoom and drawing are not compatible and cannot be enabled simultanelously

        :param flag: Enable drawing mode disabling zoom and picking mode
        :type flag: boolean, default True
        :param shape: Type of item to be drawn (line, hline, vline, rectangle...)
        :type shape: string (default polygon)
        :param label: Associated text (for identifying the signals)
        :type label: string, default None
        :param color: The color to use to draw the selection area
        :type color: string ("#RRGGBB") or 4 column unsigned byte array or
                     one of the predefined color names defined in Colors.py
        """
        self._plot.setDrawModeEnabled(flag=flag, shape=shape, label=label,
                                      color=color, **kw)

    def addItem(self, xdata, ydata, legend=None, info=None,
                replot=True, replace=False,
                shape="polygon", **kw):
        #expected to receive the same parameters as the signal
        if legend is None:
            key = "Unnamed Item 1.1"
        else:
            key = str(legend)
        if info is None:
            info = {}
        item = self._plot.addItem(xdata, ydata,
                                  legend=legend,
                                  info=info,
                                  shape=shape,
                                  **kw)
        info['plot_handle'] = item
        parameters = kw
        label = kw.get('label', legend)
        parameters['shape'] = shape
        parameters['label'] = label

        if legend in self._itemList:
            idx = self._itemList.index(legend)
            del self._itemList[idx]
        self._itemList.append(legend)
        self._itemDict[legend] = { 'x':xdata,
                                   'y':ydata,
                                   'legend':legend,
                                   'info':info,
                                   'parameters':parameters}
        return legend

    def removeItem(self, legend, replot=True):
        if legend is None:
            return
        if legend in self._itemList:
            idx = self._itemList.index(legend)
            del self._itemList[idx]
        if legend in self._itemDict:
            handle = self._itemDict[legend]['info'].get('plot_handle', None)
            del self._itemDict[legend]
            if handle is not None:
                self._plot.removeItem(handle, replot=replot)

    def getDrawMode(self):
        """
        Return a dictionary (or None) with the parameters passed when setting
        the draw mode.
        :key shape: The shape being drawn
        :key label: Associated text (or None)
        and any other info
        """
        return self._plot.getDrawMode()

    def addCurve(self, x, y, legend=None, info=None, replace=False, replot=True,
                 color=None, symbol=None, linestyle=None,
                 xlabel=None, ylabel=None, yaxis=None,
                 xerror=None, yerror=None, z=None, selectable=None, **kw):
        # Convert everything to arrays (not forcing type) in order to avoid
        # problems at unexpected places: missing min or max attributes, problem
        # when using numpy.nonzero on lists, ...
        received_symbol = symbol
        received_linestyle = linestyle
        x = numpy.asarray(x)
        y = numpy.asarray(y)
        if "line_style" in kw:
            print("DEPRECATION WARNING: line_style deprecated, use linestyle")
        if legend is None:
            key = "Unnamed curve 1.1"
        else:
            key = str(legend)
        if info is None:
            info = {}
            if key in self._curveDict:
                # prevent curves from changing attributes when updated
                oldInfo = self._curveDict[key][3]
                for savedKey in ["xlabel", "ylabel",
                                 "plot_symbol", "plot_color",
                                 "plot_linestyle", "plot_fill",
                                 "plot_yaxis"]:
                    info[savedKey] = oldInfo[savedKey]
        if xlabel is None:
            xlabel = info.get('xlabel', 'X')
        if ylabel is None:
            ylabel = info.get('ylabel', 'Y')
        info['xlabel'] = str(xlabel)
        info['ylabel'] = str(ylabel)
        if xerror is None:
            xerror = info.get("sigmax", xerror)
        info['sigmax'] = xerror
        if yerror is None:
            yerror = info.get("sigmay", yerror)
        info['sigmay'] = yerror

        if replace:
            self._curveList = []
            self._curveDict = {}
            self._colorIndex = 0
            self._styleIndex = 0
            self._plot.clearCurves()

        if key in self._curveList:
            idx = self._curveList.index(key)
            self._curveList[idx] = key
            handle = self._curveDict[key][3].get('plot_handle', None)
            if handle is not None:
                # this can give errors if it is not present in the plot
                self._plot.removeCurve(key, replot=False)
                if received_symbol is None:
                    symbol = self._curveDict[key][3].get('plot_symbol', symbol)
                if color is None:
                    color = self._curveDict[key][3].get('plot_color', color)
                if received_linestyle is None:
                    linestyle = self._curveDict[key][3].get( \
                                        'plot_linestyle', linestyle)
        else:
            self._curveList.append(key)
        #print("TODO: Here we can add properties to the info dictionary")
        #print("For instance, color, symbol, style and width if not present")
        #print("They could come in **kw")
        #print("The actual plotting stuff should only take care of handling")
        #print("logarithmic filtering if needed")
        # deal with the fill
        fill = info.get("plot_fill", False)
        fill = kw.get("fill", fill)
        info["plot_fill"] = fill

        if yaxis is None:
            yaxis = info.get("plot_yaxis", "left")
        info["plot_yaxis"] = yaxis

        # deal with the symbol
        if received_symbol is None:
            symbol = info.get("plot_symbol", symbol)

        if self._plotPoints and (received_symbol is None):
            if symbol in [None, "", " "]:
                symbol = 'o'
        elif symbol == "":
            #symbol = None
            pass
        info["plot_symbol"] = symbol
        if color is None:
            color = info.get("plot_color", color)

        if received_linestyle is None:
            linestyle = info.get("plot_linestyle", linestyle)

        if self._plotLines and (received_linestyle is None):
            if (color is None) and (linestyle is None):
                color, linestyle = self._getColorAndStyle()
            elif linestyle in [None, " ", ""]:
                linestyle = '-'
        elif received_linestyle is None:
            linestyle = ' '
        elif linestyle is None:
            linestyle = ' '

        if (color is None) and (linestyle is None):
            color, linestyle = self._getColorAndStyle()
        elif linestyle is None:
            dummy, linestyle = self._getColorAndStyle()
        elif color is None:
            color, dummy = self._getColorAndStyle()

        info["plot_color"] = color
        info["plot_linestyle"] = linestyle
        if self.isXAxisLogarithmic() or self.isYAxisLogarithmic():
            if hasattr(color, "size"):
                xplot, yplot, colorplot = self.logFilterData(x, y, color=color)
            else:
                xplot, yplot, colorplot = self.logFilterData(x, y, color=None)
                colorplot = color
        else:
            xplot, yplot = x, y
            colorplot = color

        if z is None:
            info["plot_z"] = info.get("plot_z", 1)
        else:
            info["plot_z"] = z

        if selectable is None:
            selectable = info.get("plot_selectable", True)
        info["plot_selectable"] = selectable
        if len(xplot):
            curveHandle = self._plot.addCurve(xplot, yplot, key, info,
                                              replot=False, replace=replace,
                                              color=colorplot,
                            symbol=info["plot_symbol"],
                            linestyle=info["plot_linestyle"],
                            xlabel=info["xlabel"],
                            ylabel=info["ylabel"],
                            yaxis=yaxis,
                            xerror=xerror,
                            yerror=yerror,
                            z=info["plot_z"],
                            selectable=info["plot_selectable"],
                            **kw)
            info['plot_handle'] = curveHandle
        else:
            info['plot_handle'] = key
        self._curveDict[key] = [x, y, key, info]

        if len(self._curveList) == 1:
            if self.isActiveCurveHandlingEnabled():
                self._plot.setGraphXLabel(info["xlabel"])
                self._plot.setGraphYLabel(info["ylabel"])
                self.setActiveCurve(key)

        if self.isCurveHidden(key):
            self._plot.removeCurve(key, replot=False)
        if replot:
            # We ask for a zoom reset in order to handle the plot scaling
            # if the user does not want that, autoscale of the different
            # axes has to be set to off.
            self.resetZoom()
            #self.replot()
        return key

    def addImage(self, data, legend=None, info=None,
                 replace=True, replot=True,
                 xScale=None, yScale=None, z=None,
                 selectable=False, draggable=False,
                 colormap=None, pixmap=None, **kw):
        """
        :param data: (nrows, ncolumns) data or (nrows, ncolumns, RGBA) ubyte array
        :type data: numpy.ndarray
        :param legend: The legend to be associated to the curve
        :type legend: string or None
        :param info: Dictionary of information associated to the image
        :type info: dict or None
        :param replace: Flag to indicate if already existing images are to be deleted
        :type replace: boolean default True
        :param replot: Flag to indicate plot is to be immediately updated
        :type replot: boolean default True
        :param xScale: Two floats defining the x scale
        :type xScale: list or numpy.ndarray
        :param yScale: Two floats defining the y scale
        :type yScale: list or numpy.ndarray
        :param z: level at which the image is to be located (to allow overlays).
        :type z: A number bigger than or equal to zero (default)
        :param selectable: Flag to indicate if the image can be selected
        :type selectable: boolean, default False
        :param draggable: Flag to indicate if the image can be moved
        :type draggable: boolean, default False
        :param colormap: Dictionary describing the colormap to use (or None)
        :type colormap: Dictionary or None (default). Ignored if data is RGB(A)
        :param pixmap: Pixmap representation of the data (if any)
        :type pixmap: (nrows, ncolumns, RGBA) ubyte array or None (default)
        :returns: The legend/handle used by the backend to univocally access it.
        """
        if legend is None:
            key = "Unnamed Image 1.1"
        else:
            key = str(legend)
        if info is None:
            info = {}
        xlabel = info.get('xlabel', 'Column')
        ylabel = info.get('ylabel', 'Row')
        if 'xlabel' in kw:
            info['xlabel'] = kw['xlabel']
        if 'ylabel' in kw:
            info['ylabel'] = kw['ylabel']
        info['xlabel'] = str(xlabel)
        info['ylabel'] = str(ylabel)

        if xScale is None:
            xScale = info.get("plot_xScale", None)
        if yScale is None:
            yScale = info.get("plot_yScale", None)
        if z is None:
            z = info.get("plot_z", 0)

        if replace:
            self._imageList = []
            self._imageDict = {}
        if pixmap is not None:
            dataToSend = pixmap
        else:
            dataToSend = data
        if data is not None:
            imageHandle = self._plot.addImage(dataToSend, legend=key, info=info,
                                              replot=False, replace=replace,
                                              xScale=xScale, yScale=yScale,
                                              z=z,
                                              selectable=selectable,
                                              draggable=draggable,
                                              colormap=colormap,
                                              **kw)
            info['plot_handle'] = imageHandle
        else:
            info['plot_handle'] = key
        info["plot_xScale"] = xScale
        info["plot_yScale"] = yScale
        info["plot_z"] = z
        info["plot_selectable"] = selectable
        info["plot_draggable"] = draggable
        info["plot_colormap"] = colormap
        self._imageDict[key] = [data, key, info, pixmap]
        if len(self._imageDict) == 1:
            self.setActiveImage(key)
        if replot:
            # We ask for a zoom reset in order to handle the plot scaling
            # if the user does not want that, autoscale of the different
            # axes has to be set to off.
            self.resetZoom()
            #self.replot()
        return key

    def removeCurve(self, legend, replot=True):
        """
        Remove the curve associated to the supplied legend from the graph.
        The graph will be updated if replot is true.
        :param legend: The legend associated to the curve to be deleted
        :type legend: string or None
        :param replot: Flag to indicate plot is to be immediately updated
        :type replot: boolean default True
        """
        if legend is None:
            return
        if legend in self._curveList:
            idx = self._curveList.index(legend)
            del self._curveList[idx]
        if legend in self._curveDict:
            handle = self._curveDict[legend][3].get('plot_handle', None)
            del self._curveDict[legend]
            if handle is not None:
                self._plot.removeCurve(handle, replot=replot)
        if not len(self._curveList):
            self._colorIndex = 0
            self._styleIndex = 0

    def removeImage(self, legend, replot=True):
        """
        Remove the image associated to the supplied legend from the graph.
        The graph will be updated if replot is true.
        :param legend: The legend associated to the image to be deleted
        :type legend: string or handle
        :param replot: Flag to indicate plot is to be immediately updated
        :type replot: boolean default True
        """
        if legend is None:
            return
        if legend in self._imageList:
            idx = self._imageList.index(legend)
            del self._imageList[idx]
        if legend in self._imageDict:
            handle = self._imageDict[legend][2].get('plot_handle', None)
            del self._imageDict[legend]
            if handle is not None:
                self._plot.removeImage(handle, replot=replot)
        return

    def getActiveCurve(self, just_legend=False):
        """
        :param just_legend: Flag to specify the type of output required
        :type just_legend: boolean
        :return: legend of the active curve or list [x, y, legend, info]
        :rtype: string or list

        Function to access the graph currently active curve.
        It returns None in case of not having an active curve.

        Default output has the form:
            xvalues, yvalues, legend, dict
            where dict is a dictionary containing curve info.
            For the time being, only the plot labels associated to the
            curve are warranted to be present under the keys xlabel, ylabel.

        If just_legend is True:
            The legend of the active curve (or None) is returned.
        """
        if not self.isActiveCurveHandlingEnabled():
            return None
        if self._activeCurve not in self._curveDict:
            self._activeCurve = None
        if just_legend:
            return self._activeCurve
        if self._activeCurve is None:
            return None
        else:
            return self._curveDict[self._activeCurve] * 1

    def getActiveImage(self, just_legend=False):
        """
        Function to access the plot currently active image.
        It returns None in case of not having an active image.

        Default output has the form:
            data, legend, dict, pixmap
            where dict is a dictionary containing image info.
            For the time being, only the plot labels associated to the
            image are warranted to be present under the keys xlabel, ylabel.

        If just_legend is True:
            The legend of the active imagee (or None) is returned.

        :param just_legend: Flag to specify the type of output required
        :type just_legend: boolean
        :return: legend of the active image or list [data, legend, info, pixmap]
        :rtype: string or list
        """
        if self._activeImage not in self._imageDict:
            self._activeImage = None
        if just_legend:
            return self._activeImage
        if self._activeImage is None:
            return None
        else:
            return self._imageDict[self._activeImage] * 1

    def getAllCurves(self, just_legend=False):
        """
        :param just_legend: Flag to specify the type of output required
        :type just_legend: boolean
        :return: legend of the curves or list [[x, y, legend, info], ...]
        :rtype: list of strings or list of curves

        It returns an empty list in case of not having any curve.
        If just_legend is False:
            It returns a list of the form:
                [[xvalues0, yvalues0, legend0, dict0],
                 [xvalues1, yvalues1, legend1, dict1],
                 [...],
                 [xvaluesn, yvaluesn, legendn, dictn]]
            or just an empty list.
        If just_legend is True:
            It returns a list of the form:
                [legend0, legend1, ..., legendn]
            or just an empty list.
        """
        output = []
        keys = list(self._curveDict.keys())
        for key in self._curveList:
            if key in keys:
                if self.isCurveHidden(key):
                    continue
                if just_legend:
                    output.append(key)
                else:
                    output.append(self._curveDict[key])
        return output

    def getCurve(self, legend):
        """
        :param legend: legend associated to the curve
        :type legend: boolean
        :return: list [x, y, legend, info]
        :rtype: list

        Function to access the graph specified curve.
        It returns None in case of not having the curve.

        Default output has the form:
            xvalues, yvalues, legend, info
            where info is a dictionary containing curve info.
            For the time being, only the plot labels associated to the
            curve are warranted to be present under the keys xlabel, ylabel.
        """
        if legend in self._curveDict:
            return self._curveDict[legend] * 1
        else:
            return None

    def getImage(self, legend):
        """
        :param legend: legend associated to the curve
        :type legend: boolean
        :return: list [image, legend, info, pixmap]
        :rtype: list

        Function to access the graph currently active curve.
        It returns None in case of not having an active curve.

        Default output has the form:
            image, legend, info, pixmap
            where info is a dictionary containing image information.
        """
        if legend in self._imageDict:
            return self._imageDict[legend] * 1
        else:
            return None

    def _getAllLimits(self):
        """
        Internal method to retrieve the limits based on the curves, not
        on the plot. It might be of use to reset the zoom when one of the
        X or Y axes is not set to autoscale.
        """
        keys = list(self._curveDict.keys())
        if not len(keys):
            return 0.0, 0.0, 100., 100.
        xmin = None
        ymin = None
        xmax = None
        ymax = None
        for key in keys:
            x = self._curveDict[key][0]
            y = self._curveDict[key][1]
            if xmin is None:
                xmin = x.min()
            else:
                xmin = min(xmin, x.min())
            if xmax is None:
                xmax = x.max()
            else:
                xmax = max(xmax, x.max())
            if ymin is None:
                ymin = y.min()
            else:
                ymin = min(ymin, y.min())
            if ymax is None:
                ymax = y.max()
            else:
                ymax = max(ymax, y.max())
        return xmin, ymin, xmax, ymax

    def saveGraph(self, filename, fileFormat='svg', dpi=None, **kw):
        """
        :param fileName: Destination
        :type fileName: String or StringIO or BytesIO
        :param fileFormat:  String specifying the format
        :type fileFormat: String (default 'svg')
        """
        return self._plot.saveGraph(filename,
                                    fileFormat=fileFormat,
                                    dpi=dpi,
                                    **kw)

    def setActiveCurve(self, legend, replot=True):
        """
        Funtion to request the plot window to set the curve with the specified legend
        as the active curve.
        :param legend: The legend associated to the curve
        :type legend: string
        """
        if not self.isActiveCurveHandlingEnabled():
            return
        oldActiveCurve = self.getActiveCurve(just_legend=True)
        key = str(legend)
        if key in self._curveDict.keys():
            self._activeCurve = key
        if self._activeCurve == oldActiveCurve:
            # the labels may need to be updated!!!!
            return self._activeCurve
        self._plot.setActiveCurve(self._activeCurve, replot=replot)
        return self._activeCurve

    def setActiveCurveColor(self, color="#000000"):
        if color is None:
            color = "black"
        if color in self.colorDict:
            color = self.colorDict[color]
        self._activeCurveColor = color
        self._plot.setActiveCurveColor(color)

    def setActiveImage(self, legend, replot=True):
        """
        Funtion to request the plot window to set the image with the specified legend
        as the active image.
        :param legend: The legend associated to the image
        :type legend: string
        """
        oldActiveImage = self.getActiveImage(just_legend=True)
        key = str(legend)
        if key in self._imageDict.keys():
            self._activeImage = key
        self._plot.setActiveImage(self._activeImage, replot=replot)
        return self._activeImage

    def invertYAxis(self, flag=True):
        self._plot.invertYAxis(flag)

    def isYAxisInverted(self):
        return self._plot.isYAxisInverted()

    def isYAxisLogarithmic(self):
        if self._logY:
            return True
        else:
            return False

    def isXAxisLogarithmic(self):
        if self._logX:
            return True
        else:
            return False

    def setYAxisLogarithmic(self, flag):
        if flag:
            if self._logY:
                if DEBUG:
                    print("y axis was already in log mode")
            else:
                self._logY = True
                if DEBUG:
                    print("y axis was in linear mode")
                self._plot.clearCurves()
                # matplotlib 1.5 crashes if the log set is made before
                # the call to self._update()
                # TODO: Decide what is better for other backends
                if hasattr(self._plot, "matplotlibVersion"):
                    if self._plot.matplotlibVersion < "1.5":
                        self._plot.setYAxisLogarithmic(self._logY)
                        self._update()
                    else:
                        self._update()
                        self._plot.setYAxisLogarithmic(self._logY)
                else:
                    self._plot.setYAxisLogarithmic(self._logY)
                    self._update()
        else:
            if self._logY:
                if DEBUG:
                    print("y axis was in log mode")
                self._logY = False
                self._plot.clearCurves()
                self._plot.setYAxisLogarithmic(self._logY)
                self._update()
            else:
                if DEBUG:
                    print("y axis was already linear mode")
        return

    def setXAxisLogarithmic(self, flag):
        if flag:
            if self._logX:
                if DEBUG:
                    print("x axis was already in log mode")
            else:
                self._logX = True
                if DEBUG:
                    print("x axis was in linear mode")
                self._plot.clearCurves()
                # matplotlib 1.5 crashes if the log set is made before
                # the call to self._update()
                # TODO: Decide what is better for other backends
                if hasattr(self._plot, "matplotlibVersion"):
                    if self._plot.matplotlibVersion < "1.5":
                        self._plot.setXAxisLogarithmic(self._logX)
                        self._update()
                    else:
                        self._update()
                        self._plot.setXAxisLogarithmic(self._logX)
                else:
                    self._plot.setXAxisLogarithmic(self._logX)
                    self._update()
        else:
            if self._logX:
                if DEBUG:
                    print("x axis was in log mode")
                self._logX = False
                self._plot.setXAxisLogarithmic(self._logX)
                self._update()
            else:
                if DEBUG:
                    print("x axis was already linear mode")
        return

    def logFilterData(self, x, y, xLog=None, yLog=None, color=None):
        if xLog is None:
            xLog = self._logX
        if yLog is None:
            yLog = self._logY

        if xLog and yLog:
            idx = numpy.nonzero((x > 0) & (y > 0))[0]
            x = numpy.take(x, idx)
            y = numpy.take(y, idx)
        elif yLog:
            idx = numpy.nonzero(y > 0)[0]
            x = numpy.take(x, idx)
            y = numpy.take(y, idx)
        elif xLog:
            idx = numpy.nonzero(x > 0)[0]
            x = numpy.take(x, idx)
            y = numpy.take(y, idx)
        if isinstance(color, numpy.ndarray):
            colors = numpy.zeros((x.size, 4), color.dtype)
            colors[:, 0] = color[idx, 0]
            colors[:, 1] = color[idx, 1]
            colors[:, 2] = color[idx, 2]
            colors[:, 3] = color[idx, 3]
        else:
            colors = color
        return x, y, colors

    def _update(self):
        if DEBUG:
            print("_update called")
        curveList = self.getAllCurves()
        activeCurve = self.getActiveCurve(just_legend=True)
        #self._plot.clearCurves()
        for curve in curveList:
            x, y, legend, info = curve[0:4]
            self.addCurve(x, y, legend, info=info,
                          replace=False, replot=False)
        if len(curveList):
            if activeCurve not in curveList:
                activeCurve = curveList[0][2]
            self.setActiveCurve(activeCurve)
        self.replot()

    def replot(self):
        if DEBUG:
            print("replot called")
        if self.isXAxisLogarithmic() or self.isYAxisLogarithmic():
            for image in self._imageDict.keys():
                self._plot.removeImage(image[1])
        if hasattr(self._plot, 'replot_'):
            self._plot.replot_()
        else:
            self._plot.replot()

    def clear(self, replot=True):
        self._curveList = []
        self._curveDict = {}
        self._colorIndex = 0
        self._styleIndex = 0
        self._markerDict = {}
        self._imageList = []
        self._imageDict = {}
        self._markerList = []
        self._plot.clear()
        if replot:
            self.replot()

    def clearCurves(self, replot=True):
        self._curveList = []
        self._curveDict = {}
        self._colorIndex = 0
        self._styleIndex = 0
        self._plot.clearCurves()
        if replot:
            self.replot()

    def clearImages(self, replot=True):
        """
        Clear all images from the plot. Not the curves or markers.
        """
        self._imageList = []
        self._imageDict = {}
        self._plot.clearImages()
        if replot:
            self.replot()
        return

    def getDataMargins(self):
        """Get the default data margin ratios, see :meth:`setDataMargins`.

        :return: The margin ratios for each side (xMin, xMax, yMin, yMax).
        :rtype: A 4-tuple of floats.
        """
        return self._defaultDataMargins

    def setDataMargins(self, xMinMargin=0., xMaxMargin=0.,
                       yMinMargin=0., yMaxMargin=0.):
        """Set the default data margins to use in :meth:`resetZoom`.

        Set the default ratios of margins (as floats) to add around the data
        inside the plot area for each side.
        """
        self._defaultDataMargins = (xMinMargin, xMaxMargin,
                                    yMinMargin, yMaxMargin)

    def resetZoom(self, dataMargins=None):
        if dataMargins is None:
            dataMargins = self._defaultDataMargins
        self._plot.resetZoom(dataMargins)

    def setXAxisAutoScale(self, flag=True):
        self._plot.setXAxisAutoScale(flag)

    def setYAxisAutoScale(self, flag=True):
        self._plot.setYAxisAutoScale(flag)

    def isXAxisAutoScale(self):
        return self._plot.isXAxisAutoScale()

    def isYAxisAutoScale(self):
        return self._plot.isYAxisAutoScale()

    def getGraphTitle(self):
        return self._plot.getGraphTitle()

    def getGraphXLabel(self):
        return self._plot.getGraphXLabel()

    def getGraphYLabel(self):
        return self._plot.getGraphYLabel()

    def setGraphYLimits(self, ymin, ymax, replot=False):
        self._plot.setGraphYLimits(ymin, ymax)
        if replot:
            self.replot()

    def setGraphXLimits(self, xmin, xmax, replot=False):
        self._plot.setGraphXLimits(xmin, xmax)
        if replot:
            self.replot()

    def getGraphXLimits(self):
        """
        Get the graph X (bottom) limits.
        :return:  Minimum and maximum values of the X axis
        """
        if hasattr(self._plot, "getGraphXLimits"):
            xmin, xmax = self._plot.getGraphXLimits()
        else:
            xmin, ymin, xmax, ymax = self._getAllLimits()
        return xmin, xmax

    def getGraphYLimits(self):
        """
        Get the graph Y (left) limits.
        :return:  Minimum and maximum values of the X axis
        """
        if hasattr(self._plot, "getGraphYLimits"):
            ymin, ymax = self._plot.getGraphYLimits()
        else:
            xmin, ymin, xmax, ymax = self._getAllLimits()
        return ymin, ymax

    # Title and labels
    def setGraphTitle(self, title=""):
        self._plot.setGraphTitle(title)

    def setGraphXLabel(self, label="X"):
        self._plot.setGraphXLabel(label)

    def setGraphYLabel(self, label="Y"):
        self._plot.setGraphYLabel(label)

    # Marker handling
    def insertXMarker(self, x, legend=None,
                      text=None,
                      color=None,
                      selectable=False,
                      draggable=False,
                      **kw):
        """
        kw ->symbol
        """
        if DEBUG:
            print("Received legend = %s" % legend)
        if text is None:
            text = kw.get("label", None)
            if text is not None:
                print("insertXMarker deprecation warning: Use 'text' instead of 'label'")
        if color is None:
            color = self.colorDict['black']
        elif color in self.colorDict:
            color = self.colorDict[color]
        if legend is None:
            i = 0
            legend = "Unnamed X Marker %d" % i
            while legend in self._markerList:
                i += 1
                legend = "Unnamed X Marker %d" % i

        if legend in self._markerList:
            self.removeMarker(legend)
        marker = self._plot.insertXMarker(x, legend,
                                          text=text,
                                          color=color,
                                          selectable=selectable,
                                          draggable=draggable,
                                          **kw)
        self._markerList.append(legend)
        self._markerDict[legend] = kw
        self._markerDict[legend]['marker'] = marker
        return marker

    def insertYMarker(self, y,
                      legend=None,
                      text=None,
                      color=None,
                      selectable=False,
                      draggable=False,
                      **kw):
        """
        kw -> color, symbol
        """
        if text is None:
            text = kw.get("label", None)
            if text is not None:
                print("insertYMarker deprecation warning: Use 'text' instead of 'label'")
        if color is None:
            color = self.colorDict['black']
        elif color in self.colorDict:
            color = self.colorDict[color]
        if legend is None:
            i = 0
            legend = "Unnamed Y Marker %d" % i
            while legend in self._markerList:
                i += 1
                legend = "Unnamed Y Marker %d" % i
        if legend in self._markerList:
            self.removeMarker(legend)
        marker = self._plot.insertYMarker(y, legend=legend,
                                          text=text,
                                          color=color,
                                          selectable=selectable,
                                          draggable=draggable,
                                          **kw)
        self._markerList.append(legend)
        self._markerDict[legend] = kw
        self._markerDict[legend]['marker'] = marker
        return marker

    def insertMarker(self, x, y, legend=None,
                     text=None,
                     color=None,
                     selectable=False,
                     draggable=False,
                     symbol=None,
                     constraint=None,
                     **kw):
        if text is None:
            text = kw.get("label", None)
            if text is not None:
                print("insertMarker deprecation warning: Use 'text' instead of 'label'")
        if color is None:
            color = self.colorDict['black']
        elif color in self.colorDict:
            color = self.colorDict[color]
        if legend is None:
            i = 0
            legend = "Unnamed Marker %d" % i
            while legend in self._markerList:
                i += 1
                legend = "Unnamed Marker %d" % i

        if constraint is not None and not callable(constraint):
            # Then it must be a string
            if hasattr(constraint, 'lower'):
                if constraint.lower().startswith('h'):
                    constraint = lambda xData, yData: (xData, y)
                elif constraint.lower().startswith('v'):
                    constraint = lambda xData, yData: (x, yData)
                else:
                    raise ValueError(
                        "Unsupported constraint name: %s" % constraint)
            else:
                raise ValueError("Unsupported constraint")

        if legend in self._markerList:
            self.removeMarker(legend)
        marker = self._plot.insertMarker(x, y, legend=legend,
                                          text=text,
                                          color=color,
                                          selectable=selectable,
                                          draggable=draggable,
                                          symbol=symbol,
                                          constraint=constraint,
                                          **kw)
        self._markerList.append(legend)
        self._markerDict[legend] = kw
        self._markerDict[legend]['marker'] = marker
        return marker

    def keepDataAspectRatio(self, flag=True):
        """
        :param flag:  True to respect data aspect ratio
        :type flag: Boolean, default True
        """
        self._plot.keepDataAspectRatio(flag=flag)

    def clearMarkers(self):
        self._markerDict = {}
        self._markerList = []
        self._plot.clearMarkers()
        self.replot()

    def removeMarker(self, marker):
        if marker in self._markerList:
            idx = self._markerList.index(marker)
            del self._markerList[idx]
            try:
                self._plot.removeMarker(self._markerDict[marker]['marker'])
                del self._markerDict[marker]
            except KeyError:
                if DEBUG:
                    print("Marker was not present %s"  %\
                          self._markerDict[marker]['marker'])

    def setMarkerFollowMouse(self, marker, boolean):
        raise NotImplemented("Not necessary?")
        if marker not in self._markerList:
            raise ValueError("Marker %s not defined" % marker)
        pass

    def enableMarkerMode(self, flag):
        raise NotImplemented("Not necessary?")
        pass

    def isMarkerModeEnabled(self, flag):
        raise NotImplemented("Not necessary?")
        pass

    def showGrid(self, flag=True):
        if DEBUG:
            print("Plot showGrid called")
        self._plot.showGrid(flag)

    # colormap related functions
    def getDefaultColormap(self):
        """
        Return the colormap that will be applied by the backend to an image
        if no colormap is applied to it.
        A colormap is a dictionary with the keys:
        :type name: string
        :type normalization: string (linear, log)
        :type autoscale: boolean
        :type vmin: float, minimum value
        :type vmax: float, maximum value
        :type colors: integer (typically 256)
        """
        return self._plot.getDefaultColormap()

    def setDefaultColormap(self, colormap=None):
        """
        Sets the colormap that will be applied by the backend to an image
        if no colormap is applied to it.
        A colormap is a dictionary with the keys:
        :type name: string
        :type normalization: string (linear, log)
        :type autoscale: boolean
        :type vmin: float, minimum value
        :type vmax: float, maximum value
        :type colors: integer (typically 256)

        If None is passed, the backend will reset to its default colormap.
        """
        self._plot.setDefaultColormap(colormap)

    def getSupportedColormaps(self):
        """
        Get a list of strings with the colormap names supported by the backend.
        The list should at least contain and start by:
        ['gray', 'reversed gray', 'temperature', 'red', 'green', 'blue']
        """
        return self._plot.getSupportedColormaps()

    def hideCurve(self, legend, flag=True, replot=True):
        if flag:
            self._plot.removeCurve(legend, replot=replot)
            if legend not in self._hiddenCurves:
                self._hiddenCurves.append(legend)
        else:
            while legend in self._hiddenCurves:
                idx = self._hiddenCurves.index(legend)
                del self._hiddenCurves[idx]
            if legend in self._curveDict:
                x, y, legend, info = self._curveDict[legend][0:4]
                self.addCurve(x, y, legend, info, replot=replot)

    def isCurveHidden(self, legend):
        return legend in self._hiddenCurves

    def dataToPixel(self, x=None, y=None, axis="left"):
        """
        Convert a position in data space to a position in pixels in the widget.

        :param float x: The X coordinate in data space. If None (default)
                            the middle position of the displayed data is used.
        :param float y: The Y coordinate in data space. If None (default)
                            the middle position of the displayed data is used.
        :param str axis: The Y axis to use for the conversion
                         ('left' or 'right').
        :returns: The corresponding position in pixels or
                  None if the data position is not in the displayed area.
        :rtype: A tuple of 2 floats: (xPixel, yPixel) or None.
        """
        return self._plot.dataToPixel(x, y, axis=axis)

    def pixelToData(self, x=None, y=None, axis="left"):
        """
        Convert a position in pixels in the widget to a position in
        the data space.

        :param float x: The X coordinate in pixels. If None (default)
                            the center of the widget is used.
        :param float y: The Y coordinate in pixels. If None (default)
                            the center of the widget is used.
        :param str axis: The Y axis to use for the conversion
                         ('left' or 'right').
        :returns: The corresponding position in data space or
                  None if the pixel position is not in the plot area.
        :rtype: A tuple of 2 floats: (xData, yData) or None.
        """
        return self._plot.pixelToData(x, y, axis=axis)

    def setGraphCursor(self, flag=None, color=None,
                       linewidth=None, linestyle=None):
        """
        Toggle the display of a crosshair cursor and set its attributes.

        :param bool flag: Toggle the display of a crosshair cursor.
                           The crosshair cursor is hidden by default.
        :param color: The color to use for the crosshair.
        :type color: A string (either a predefined color name in Colors.py
                    or "#RRGGBB")) or a 4 columns unsigned byte array.
                    Default is black.
        :param int linewidth: The width of the lines of the crosshair.
                    Default is 1.
        :param linestyle: Type of line::
        
                - ' ' no line
                - '-' solid line
                - '--' dashed line
                - '-.' dash-dot line
                - ':' dotted line

        :type linestyle: None or one of the predefined styles.
        """
        self._plot.setGraphCursor(flag=flag, color=color,
                                  linewidth=linewidth, linestyle=linestyle)
    def getGraphCursor(self):
        """
        Returns the current state of the crosshair cursor.

        :return: None if the crosshair cursor is not active,
                 else a tuple (color, linewidth, linestyle).
        """
        return self._plot.getGraphCursor()

    # Pan support

    def pan(self, direction, factor=0.1):
        """Pan the graph in the given direction by the given factor.

        Warning: Pan of right Y axis not implemented!

        :param str direction: One of 'up', 'down', 'left', 'right'.
        :param float factor: Proportion of the range used to pan the graph.
                             Must be strictly positive.
        """
        assert direction in ('up', 'down', 'left', 'right')
        assert factor > 0.

        if direction in ('left', 'right'):
            xFactor = factor if direction == 'right' else - factor
            xMin, xMax = self.getGraphXLimits()

            xMin, xMax = _applyPan(xMin, xMax, xFactor,
                                   self.isXAxisLogarithmic())
            self.setGraphXLimits(xMin, xMax)

        else:  # direction in ('up', 'down')
            sign = -1. if self.isYAxisInverted() else 1.
            yFactor  = sign * (factor if direction == 'up' else - factor)
            yMin, yMax = self.getGraphYLimits()
            yIsLog = self.isYAxisLogarithmic()

            yMin, yMax = _applyPan(yMin, yMax, yFactor, yIsLog)
            self.setGraphYLimits(yMin, yMax)

            # TODO handle second Y axis

        self.replot()

def _applyPan(min_, max_, panFactor, isLog10):
    """Returns a new range with applied panning.

    Moves the range according to panFactor.
    If isLog10 is True, converts to log10 before moving.

    :param float min_: Min value of the data range to pan.
    :param float max_: Max value of the data range to pan.
                       Must be >= min_.
    :param float panFactor: Signed proportion of the range to use for pan.
    :param bool isLog10: True if log10 scale, False if linear scale.
    :return: New min and max value with pan applied.
    :rtype: 2-tuple of float.
    """
    if isLog10 and min_ > 0.:
        # Negative range and log scale can happen with matplotlib
        logMin, logMax = math.log10(min_), math.log10(max_)
        logOffset = panFactor * (logMax - logMin)
        newMin = pow(10., logMin + logOffset)
        newMax = pow(10., logMax + logOffset)

        # Takes care of out-of-range values
        if newMin > 0. and newMax < float('inf'):
            min_, max_ = newMin, newMax

    else:
        offset = panFactor * (max_ - min_)
        newMin, newMax = min_ + offset, max_ + offset

        # Takes care of out-of-range values
        if newMin > - float('inf') and newMax < float('inf'):
            min_, max_ = newMin, newMax
    return min_, max_

def main():
    x = numpy.arange(100.)
    y = x * x
    plot = Plot()
    plot.addCurve(x, y, "dummy")
    plot.addCurve(x + 100, -x * x, "To set Active")
    print("Active curve = ", plot.getActiveCurve())
    print("X Limits = ", plot.getGraphXLimits())
    print("Y Limits = ", plot.getGraphYLimits())
    print("All curves = ", plot.getAllCurves())
    plot.removeCurve("dummy")
    plot.setActiveCurve("To set Active")
    print("All curves = ", plot.getAllCurves())
    plot.insertXMarker(50.)

if __name__ == "__main__":
    main()