7
7
import matplotlib .pyplot as plt
8
8
import numpy as np
9
9
10
+ from .backends .mtpltlib import Matplotlib
11
+ from .backends .bkh import Bokeh
12
+
10
13
11
14
class GridStrategy (metaclass = ABCMeta ):
12
15
"""
@@ -15,8 +18,38 @@ class GridStrategy(metaclass=ABCMeta):
15
18
nearly square (nearly equal in both dimensions).
16
19
"""
17
20
18
- def __init__ (self , alignment = "center" ):
21
+ def __init__ (self , alignment = "center" , backend = "matplotlib" ):
19
22
self .alignment = alignment
23
+ self .supported_backends = ["matplotlib" , "bokeh" ]
24
+ self .library = None
25
+
26
+ assert (
27
+ backend in self .supported_backends
28
+ ), f"Library { backend } is not a supported backend."
29
+ if backend == "matplotlib" :
30
+ try :
31
+ import matplotlib
32
+ except ImportError :
33
+ print (
34
+ "matplotlib not installed. Please install it to use it with grid_strategy."
35
+ )
36
+ self .library = Matplotlib (alignment = self .alignment )
37
+
38
+ elif backend == "bokeh" :
39
+ try :
40
+ import bokeh
41
+ except ImportError :
42
+ print (
43
+ "Bokeh is not installed. Please install it to use it with grid_strategy."
44
+ )
45
+ self .library = Bokeh (alignment = self .alignment )
46
+
47
+ # elif backend == "plotly":
48
+ # try:
49
+ # import plotly
50
+ # except ImportError:
51
+ # print("plotly not installed. Please install it to use it with grid_strategy.")
52
+ # self.library = Plotly(alignment=self.alignment)
20
53
21
54
def get_grid (self , n ):
22
55
"""
@@ -30,70 +63,22 @@ def get_grid(self, n):
30
63
where each x would be a subplot.
31
64
"""
32
65
66
+ if n < 0 :
67
+ raise ValueError
33
68
grid_arrangement = self .get_grid_arrangement (n )
34
- return self .get_gridspec (grid_arrangement )
69
+ return self .get_figures (grid_arrangement )
35
70
36
71
@classmethod
37
72
@abstractmethod
38
73
def get_grid_arrangement (cls , n ): # pragma: nocover
39
74
pass
40
75
41
- def get_gridspec (self , grid_arrangement ):
76
+ def get_figures (self , grid_arrangement ):
42
77
nrows = len (grid_arrangement )
43
78
ncols = max (grid_arrangement )
44
79
45
80
# If it has justified alignment, will not be the same as the other alignments
46
81
if self .alignment == "justified" :
47
- return self ._justified (nrows , grid_arrangement )
48
- else :
49
- return self ._ragged (nrows , ncols , grid_arrangement )
50
-
51
- def _justified (self , nrows , grid_arrangement ):
52
- ax_specs = []
53
- num_small_cols = np .lcm .reduce (grid_arrangement )
54
- gs = gridspec .GridSpec (
55
- nrows , num_small_cols , figure = plt .figure (constrained_layout = True )
56
- )
57
- for r , row_cols in enumerate (grid_arrangement ):
58
- skip = num_small_cols // row_cols
59
- for col in range (row_cols ):
60
- s = col * skip
61
- e = s + skip
62
-
63
- ax_specs .append (gs [r , s :e ])
64
- return ax_specs
65
-
66
- def _ragged (self , nrows , ncols , grid_arrangement ):
67
- if len (set (grid_arrangement )) > 1 :
68
- col_width = 2
82
+ return self .library ._justified (nrows , grid_arrangement )
69
83
else :
70
- col_width = 1
71
-
72
- gs = gridspec .GridSpec (
73
- nrows , ncols * col_width , figure = plt .figure (constrained_layout = True )
74
- )
75
-
76
- ax_specs = []
77
- for r , row_cols in enumerate (grid_arrangement ):
78
- # This is the number of missing columns in this row. If some rows
79
- # are a different width than others, the column width is 2 so every
80
- # column skipped at the beginning is also a missing slot at the end.
81
- if self .alignment == "left" :
82
- # This is left-justified (or possibly full justification)
83
- # so no need to skip anything
84
- skip = 0
85
- elif self .alignment == "right" :
86
- # Skip two slots for every missing plot - right justified.
87
- skip = (ncols - row_cols ) * 2
88
- else :
89
- # Defaults to centered, as that is the default value for the class.
90
- # Skip one for each missing column - centered
91
- skip = ncols - row_cols
92
-
93
- for col in range (row_cols ):
94
- s = skip + col * col_width
95
- e = s + col_width
96
-
97
- ax_specs .append (gs [r , s :e ])
98
-
99
- return ax_specs
84
+ return self .library ._ragged (nrows , ncols , grid_arrangement )
0 commit comments