myFig.py 4.65 KB
Newer Older
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
#   myplotlib v0.0.0,
#
#   @file      myFig.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   MyFig class: Overlay of matplotlib Figure class
#   It is done such that the user can concentrate on
#   the important aspect of the figure and not the
#   technical part.
#
#   @Class MyFig
#
#   @Constructor
#
#                fignum:      number of the Figure.
#                rawdata:     identifier of the raw data in G_RAWDATA.
#                reformat:    flag for reComputing plotted data
#                             from rawData.
#                debug:       flag for debugging, more verbatile.
#                (kw)args:    user defined arguments and keywords.
#
#   @section Functions
#
#            - printDebug:    print some debug information.
# 
#            - plot:          overlay of Figure.plot() function,
#                             should be overwrited by the user.
#                             Consists of
#                             - creating axes,
#                             - formating the data,
#                             - adding the axis to the figure
#                             - plotting the axis
#                             
#            - update:        This is done to update the figure
#                             in case of redraw to make sure that
#                             the attributes are up-to-date.
#                             Important for interactive work.
#
#   @section History
#
#   v 0.0.0 - MyFig class for the myplotlib module.
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
from myplotlib import D_FIGSIZE, D_INPUTARG, D_DEBUG, D_REFORMAT, D_FIGNUM
from myplotlib import DBUG, SEVR, INFO, SPCE, WARN

from myplotlib import G_RAWDATAS

from myplotlib import Figure, MyAxes


# Class MyFig Overwriting Matplotlib.fure.Figure
class MyFig(Figure):
  
    # Set the size of the Figure in inch
    # (private variable can not be updated)
    FIGSIZE = D_FIGSIZE
  
    # CONSTRUCTOR --------------------------------------------------------
    def __init__(self, fignum=D_FIGNUM,
                 inputarg=D_INPUTARG, reformat=D_REFORMAT,
                 debug=D_DEBUG, *args, **kwargs):
       
        kwargs['figsize'] = self.FIGSIZE
       
        # parent constructor
        Figure.__init__(self, *args, **kwargs)
       
        # parameters
        self.debug     = debug     # debug Flag
        self.fignum    = fignum    # number of the Figure
        self.inputArg  = inputarg  # input arguments
        self.reformat  = reformat  # re-formatted Flag
        self.formatted = False

    # DEBUG --------------------------------------------------------------
    def printDebug(self):
        className = str(self.__class__.__name__)
        print('\n' + DBUG + "  {0} PARAMETERS: ".format(className))
        print(SPCE + "          Raw data: " + str(self.inputArg))
        print(SPCE + "     ID the figure: " + str(self.fignum))
        print(SPCE + "Size of the figure: " + str(self.FIGSIZE) + ' [inch] \n')

    # PLOT ---------------------------------------------------------------
    def plot(self):
        
        try:
            self.rawdata = G_RAWDATAS[self.inputArg]
        except KeyError:
            print(SEVR + "The dataset {dataName} does not exist. --> FALSE".format(dataName=self.inputArg))
            return(False)
        
        ratio = 6. / 8.  # height/width of the axes (in inch)
        frame = [0.0, 0.0, 1.0, 1.0]  # part of the fig that is available
        
        # create an axes MyAxes with a given rect
        ax = MyAxes(self, ratio, frame)
        
        # reformat the data if needed (default True)
        if((self.reformat) or (not self.format)):
            try:
                status = ax.formatRawData(self.rawdata)
                self.formatted = True
            except (TypeError, KeyError):
                print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
                return(False)
        
        if(not status): return(False)
        
        # add the axis to the figure
        self.add_axes(ax)
        
        # try to plot the Axes
        status = ax.plotting()
        if(not status): return(False)
        
        return(True)
   
    # UPDATE -------------------------------------------------------------
    def update(self, inputarg=D_INPUTARG,
               reformat=D_REFORMAT, debug=D_DEBUG,
               *args, **kwargs):
     
        self.debug    = debug
        self.inputArg = inputarg
        self.reformat = reformat