myAxes.py 5.02 KB
Newer Older
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
1
2
3
4
5
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
Philipp Gast's avatar
Philipp Gast committed
6
#   myplotlib v3.0.1,
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
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
#
#   @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.
#
41
42
#   v 2.2.3 - Add testRawData function
#
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
43
44
45
46
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
47
48
from . import SEVR, DBUG
from . import Axes
49
from .mytool import computeFitRect
50
from . import rcParams
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
51
52
53
54


# Class MyAxes Overwriting Matplotlib.figure.Axes
class MyAxes(Axes):
55
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
56
    # CONSTRUCTOR --------------------------------------------------------
57
    def __init__(self, fig, frameRect, *args, **kwargs):
58
59
        
        self.fig = fig
60
        self.declareKeywords()
61
        
62
        rect = self.computeRect(frameRect, *args, **kwargs)
63
64
        
        # clean kwargs
65
        kwargs.pop('ratio', None)
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
66
67
        kwargs.pop('forceRatioOnWidth', None)
        kwargs.pop('forceRatioOnHeight', None)
68
        kwargs.pop('fitInFrame', None)
69
70
71
        
        # parent constructor
        Axes.__init__(self, fig, rect, **kwargs)
72
73
        
        # set a default name. Should be individualized by instance
74
        self.name = 'default'
75
76
    
    # COMPUTE RECT -------------------------------------------------------
77
    def computeRect(self, frameRect, *args, **kwargs):
78
79
        
        # Get the optional keyword
80
        ratio = kwargs.pop('ratio', None)
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
81
82
        forceRatioOnWidth = kwargs.pop('forceRatioOnWidth', None)
        forceRatioOnHeight = kwargs.pop('forceRatioOnHeight', None)
83
        fitInFrame = kwargs.pop('fitInFrame', None)
84
        
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
85
86
        # get the frame allowed
        framePosX, framePosY, frameWidth, frameHeight = frameRect
87
        
88
89
90
91
        # get the size of the figure
        figWidth  = self.fig.get_figwidth()
        figHeight = self.fig.get_figheight()
        
92
93
94
95
96
97
98
99
        if ratio is not None:
            
            if(forceRatioOnWidth):
                frameWidth = ratio * figWidth / (frameHeight * figHeight)
            elif(forceRatioOnHeight):
                frameHeight = ratio * (frameWidth * figWidth) / figHeight
            else:
                frameHeight = ratio * (frameWidth * figWidth) / figHeight
100
101
102
        
        rect = [framePosX, framePosY, frameWidth, frameHeight]
        
103
104
105
106
107
        if (fitInFrame):
            
            if ratio is None:
                ratio = rect[3]/rect[2]
            
108
            rect = computeFitRect(self, ratio, rect)
109
        
110
        return(rect)
111
112
113
    
    # DECLARE KEYWORDS -------------------------------------------------
    def declareKeywords(self):
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
114
        self.keywords = {}
115
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
116
117
    # PLOTTING -----------------------------------------------------------
    def plotting(self):
118
        return(True)
119
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
120
121
    # FORMATTING ---------------------------------------------------------
    def formatRawData(self, rawdata):
122
        return(True)
123
    
124
125
    # TESTING THE RAWDATA ------------------------------------------------
    def testRawData(self, rawdata):
126
        return(True)
127
128
    
    # UPDATE -------------------------------------------------------------
129
130
131
    def update(self, *args, **kwargs):
        
        # Because matplotlib.axes.update expect kwargs and not **kwargs ... (stupid!!)
132
133
        if args:  # catch matplotlib kwargs
            kwargs = args[0]
134
135
136
137
138
139
140
            # kw_for_axes = {key: value for (key, value) in args[0].items() if key not in self.keywords}  # Not compatible with python2.6
            
            kw_for_axes = {}
            for (key, value) in args[0].items():
                if key not in self.keywords:
                    kw_for_axes.update({key: value})
            
141
            Axes.update(self, kw_for_axes)  # update matplotlib.Axes
142
        
143
        # myplotlib update
144
145
146
147
148
149
        # self.keywords.update({key: value for (key, value) in kwargs.items() if key in self.keywords})  # Not compatible with python2.6
        
        for (key, value) in kwargs.items():
            if key in self.keywords:
                self.keywords.update({key: value})