myAxes.py 4.37 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
#   myplotlib v0.0.0,
#
#   @file      myAxes.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   MyAxes class: Overlay of matplotlib Axes class
#   It is done such that the user can concentrate on
#   the important aspect of the figure and not the
#   technical part. It consists of a constructor,
#   that requires a Figure, the ratio of the xaxis over
#   the yaxis, and the frame in which the figure
#   should be plotted.
#
#   @Class MyAxes
#
#   @Constructor(self, fig, ratio, frame, +user defined kw):
#
#   @section Functions
#
#            - plotting: this is the overwritten function
#                        from Axes. It is called by it's
#                        parent-figure's function .plot()
#
#            - formatRawData: it computes out of the rawData
#                        given as argument some quantities that
#                        are returned as a dictionary and
#                        become accessable for plotting.
#
#   @section History
#
#   v 0.0.0 - MyAxes class for the myplotlib module, consists
#             of a constructor, a pltting function and
#             formatRawData.
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
45
46
47
from ..myplotlib import SEVR, DBUG
from ..myplotlib import Axes
from ..myplotlib import rcParams
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
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


# Class MyAxes Overwriting Matplotlib.figure.Axes
class MyAxes(Axes):
  
    # CONSTRUCTOR --------------------------------------------------------
    def __init__(self, fig, ratio, frameRect, **kwargs):
      
        self.debug = kwargs.get('debug', 0)
        
        # get the frame allowed
        framePosX, framePosY, frameWidth, frameHeight = frameRect
        # get the size of the figure
        figWidth  = fig.get_figwidth()
        figHeight = fig.get_figheight()
        figDpi    = fig.get_dpi()
        
        # get the font size in inches
        fontsize = rcParams['font.size'] / figDpi
        
        # the largest width and height allowed in inches
        maxWidth = figWidth * frameWidth - 8. * (figDpi / 75.) * fontsize
        maxHeight = figHeight * frameHeight - 4. * (figDpi / 75.) * fontsize
        
        # the hypothetical width and height in inches
        # for a given aspect ratio
        hypoWidth = maxHeight / ratio
        hypoHeight = maxWidth * ratio
        
        # if the hypothetical width is larger that the
        # maximum allowed width (in inches) then chose
        # the hypothetical Height
        if (hypoWidth > maxWidth):
            height = hypoHeight / figHeight  # height in percent
            width  = maxWidth / figWidth     # width in percent
            posX   = framePosX + 4. * (figDpi / 75.) * fontsize / figWidth      # posX
            posY   = framePosY + 3. * (figDpi / 75.) * fontsize / figHeight     # posY
        else:
            height = maxHeight / figHeight
            width  = hypoWidth / figWidth
            posX   = framePosX + (frameWidth - width) / 2.
            posY   = framePosY + 3. * (figDpi / 75.) * fontsize / figHeight
        
        rect = [posX, posY, width, height]
        
        # parent constructor
        Axes.__init__(self, fig, rect, **kwargs)
    
    # PLOTTING -----------------------------------------------------------
    # the plotting function (need to be overwrite from child
    def plotting(self):
        try: 
            self.plot(self.data['xdata'], self.data['ydata'])
        except KeyError:
            print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
            return(False)
        
        return(True)
   
    # FORMATTING ---------------------------------------------------------
    def formatRawData(self, rawdata):
     
        # give value to data a dict
        # with xdata, ydata1, zdata, ydata2 ...
        try:
            self.data = {'xdata': [rawdata.data[0], rawdata.data[1]],
                         'ydata': [rawdata.data[2], rawdata.data[3]]}
        except (TypeError, KeyError):
            print(SEVR + 'The Raw Data could not be formatted --> EXIT')
            return(False)
       
        if(self.debug):
            print(DBUG + 'I formatted the raw data!\n')
            print(DBUG + 'formatted data:\n' + str(self.formattedDatas))
        
        return(True)