Commit 3b7f7a08 authored by Yori Fournier's avatar Yori Fournier
Browse files

add Weighted Grids into mytools

add some tutorials for demonstration and tests, but not yet tested.
parent 32a980db
Pipeline #478 failed with stage
in 4 minutes and 18 seconds
......@@ -16,4 +16,9 @@ from .figOneAxes import FigOneAxes
from .formatter import FormatterListToLine
from .formatter import FormatterArrayToLines
from .formatter import FormatterAppendToLines
from .mplWeightedGrid import WeightedGrid
from .mplWeightedGrid import VerticalWeightedGrid
from .mplWeightedGrid import HorizontalWeightedGrid
from .ax2dPlots import Ax2dPlots
from itertools import repeat
class Formatter(object):
def __init__(self):
......@@ -69,7 +71,7 @@ class FormatterAppendToLines(Formatter):
iindex = 1
else:
iindex = 0
labels = None
labels = []
for item in rawdata.data[iindex]:
ydatas.append([item])
......@@ -81,11 +83,13 @@ class FormatterAppendToLines(Formatter):
if self.xdataIndex is not None:
xdata = ydatas.pop(self.xdataIndex)
if labels is not None:
if labels != []:
if self.xlabel is None:
self.xlabel = labels.pop(self.xdataIndex)
else:
labels.pop(self.xdataIndex)
else:
labels = repeat(None)
else:
xdata = range(0, len(ydatas[0])) # generic xdata
......
_D_MARGIN = 0.05
_D_PADDING = 0.05
_D_POS = [0.0, 0.0, 1.0, 1.0]
from myplotlib import Grid
from myplotlib import WARN, INFO, SEVR, SPCE
class WeightedGrid(Grid):
def __init__(self, pos=_D_POS, margin=_D_MARGIN, padding=_D_PADDING, weights=None):
Grid.__init__(self, pos=pos, margin=margin, padding=padding)
# This has to be done that way because putting "weights=[]" as optional argument in the constructor
# would results into a global variable. Indeed "weights=[]" means create an array while importing.
# and since it is an array its pointer will be transmitted to "self.weights". So for any WeightedGrid
# and also for all its childs, the value self.weights will point toward the same object.
# Therefore we initialise "self.weights" within the constructor.
if weights is None:
self.weights = []
else:
self.weights = weights
def appendAxes(self, ax, axlabel, rawdata, weight=None, weights=None):
self.items.append(ax)
if weights is not None:
self.weights = weights
else:
self.weights.append(weight)
self.fig.add_axes(ax, axlabel)
self.fig.set_rawdata(self.fig.rawdata + (rawdata,))
def appendGrid(self, grid, weight=None, weights=None):
self.items.append(grid)
if weights is not None:
self.weights = weights
else:
self.weights.append(weight)
self.items[-1].fig = self.fig
def getItemWeight(self, index):
return(self.weights[index])
def getWeights(self):
return(self.weights)
class VerticalWeightedGrid(WeightedGrid):
def layout(self):
nbr_axes = len(self.items)
sum_of_weights = sum(self.weights)
if sum_of_weights > 1.:
print(WARN + "The weights you have given are larger than 100% your axes will overcross the Figure's borders")
height_avail_for_axes = self.height - (nbr_axes-1)*self.padding - 2.*self.margin
current_height = 0.0
for index in range(0, nbr_axes):
axes_height = self.weights[index] * height_avail_for_axes
new_left = self.margin + self.left
new_bottom = self.margin + self.bottom + current_height + index*self.padding
new_width = self.width - 2.*self.margin
new_height = axes_height
new_frame = [new_left, new_bottom, new_width, new_height]
self.items[index].set_position(new_frame)
current_height = current_height + axes_height
class HorizontalWeightedGrid(WeightedGrid):
def layout(self):
nbr_axes = len(self.items)
sum_of_weights = sum(self.weights)
if sum_of_weights > 1.:
print(WARN + "The weights you have given are larger than 100% your axes will overcross the Figure's borders")
width_avail_for_axes = self.width - (nbr_axes-1)*self.padding - 2.*self.margin
current_width = 0.0
for index in range(0, nbr_axes):
axes_width = self.weights[index] * width_avail_for_axes
new_left = self.margin + self.left + current_width + index*self.padding
new_bottom = self.margin + self.bottom
new_width = axes_width
new_height = self.height - 2.*self.margin
new_frame = [new_left, new_bottom, new_width, new_height]
self.items[index].set_position(new_frame)
current_width = current_width + axes_width
......@@ -2,3 +2,5 @@
./tuto001.py
./tuto002.py
./tuto003.py
./tuto004.py
./tuto005.py
......@@ -6,6 +6,8 @@ from tuto000 import script as sc000
from tuto001 import script as sc001
from tuto002 import script as sc002
from tuto003 import script as sc003
from tuto004 import script as sc004
from tuto005 import script as sc005
class TutorialsTestCase(unittest.TestCase):
......@@ -81,5 +83,13 @@ class TutorialsTestCase(unittest.TestCase):
self.assertEqual(fig.axes[0].lines[1].get_linestyle(), '-')
self.assertEqual(fig.axes[0].lines[2].get_linestyle(), u'None')
def test_tuto_004(self):
fig = sc004()
fig.plot()
def test_tuto_005(self):
fig = sc005()
fig.plot()
if __name__ == '__main__':
unittest.main()
#!/usr/bin/env python
def script(with_print=False):
import sys
sys.path.append('../../')
from myplotlib.mytool import Ax2dPlots, FormatterAppendToLines, VerticalWeightedGrid, HorizontalWeightedGrid
from myplotlib import EmptyFig, MyData
# rawdata set
data = [[0.1, 0.1, 0.12, 0.1],
[0.3, 0.2, 0.22, 0.21],
[1.0, 0.4, 0.44, 0.41],
[3.0, 0.6, 0.65, 0.59],
[10., 1.0, 1.05, 1.01],
[30., 0.9, 0.96, 0.88],
[100., 1.2, 1.27, 1.17],
[300., 1.3, 1.34, 1.31],
[1000., 1.2, 1.28, 1.22]]
# pack into MyData
rawdata = MyData(data=data, name='just-a-list')
# Create an empty figure
fig = EmptyFig(figsize=(16., 12.))
# Add a grid with margin and padding for the titles
vgrid = VerticalWeightedGrid(margin=0.07, padding=0.05)
fig.setLayout(vgrid)
# Create a formatter for the rawdata set (set the second list as x-axis)
formatter = FormatterAppendToLines(xdataIndex=0)
# Create 5 axes
ax1 = Ax2dPlots(fig, formatter=formatter)
ax2 = Ax2dPlots(fig, formatter=formatter)
ax3 = Ax2dPlots(fig, formatter=formatter)
ax4 = Ax2dPlots(fig, formatter=formatter)
ax5 = Ax2dPlots(fig, formatter=formatter)
hgrid1 = HorizontalWeightedGrid(margin=0., padding=0.05)
hgrid2 = HorizontalWeightedGrid(margin=0., padding=0.05)
vgrid.appendGrid(hgrid1, weight=0.3)
vgrid.appendGrid(hgrid2, weight=0.3)
# Add the axes into the grid
hgrid1.appendAxes(ax1, 'ax1', rawdata, weight=0.3)
hgrid1.appendAxes(ax2, 'ax2', rawdata, weight=0.7)
hgrid2.appendAxes(ax3, 'ax3', rawdata, weight=0.7)
hgrid2.appendAxes(ax4, 'ax4', rawdata, weight=0.3)
vgrid.appendAxes(ax5, 'ax5', rawdata, weight=0.4)
# Layout the grid
fig.layout.layout()
hgrid1.layout()
hgrid2.layout()
if with_print:
# save as file
fig.print2file('./tuto004.png')
return(fig)
if __name__ == '__main__':
fig = script(True)
fig.plot()
fig.print2file('./tuto004.png')
#!/usr/bin/env python
def script(with_print=False):
import sys
sys.path.append('../../')
from myplotlib.mytool import Ax2dPlots, FormatterAppendToLines
from myplotlib import EmptyFig, MyData, VerticalGrid, HorizontalGrid
# rawdata set
data = [[0.1, 0.1, 0.12, 0.1],
[0.3, 0.2, 0.22, 0.21],
[1.0, 0.4, 0.44, 0.41],
[3.0, 0.6, 0.65, 0.59],
[10., 1.0, 1.05, 1.01],
[30., 0.9, 0.96, 0.88],
[100., 1.2, 1.27, 1.17],
[300., 1.3, 1.34, 1.31],
[1000., 1.2, 1.28, 1.22]]
# pack into MyData
rawdata = MyData(data=data, name='just-a-list')
# Create an empty figure
fig = EmptyFig(figsize=(16., 12.))
# Add a grid with margin and padding for the titles
vgrid = VerticalGrid(margin=0.07, padding=0.05)
fig.setLayout(vgrid)
# Create a formatter for the rawdata set (set the second list as x-axis)
formatter = FormatterAppendToLines(xdataIndex=0)
# Create 5 axes
ax1 = Ax2dPlots(fig, formatter=formatter)
ax2 = Ax2dPlots(fig, formatter=formatter)
ax3 = Ax2dPlots(fig, formatter=formatter)
ax4 = Ax2dPlots(fig, formatter=formatter)
ax5 = Ax2dPlots(fig, formatter=formatter)
hgrid1 = HorizontalGrid(margin=0., padding=0.05)
hgrid2 = HorizontalGrid(margin=0., padding=0.05)
fig.layout.appendGrid(hgrid1)
fig.layout.appendGrid(hgrid2)
# Add the axes into the grid
hgrid1.appendAxes(ax1, 'ax1', rawdata)
hgrid1.appendAxes(ax2, 'ax2', rawdata)
hgrid2.appendAxes(ax3, 'ax3', rawdata)
hgrid2.appendAxes(ax4, 'ax4', rawdata)
fig.layout.appendAxes(ax5, 'ax6', rawdata)
# Layout the grid
fig.layout.layout()
hgrid1.layout()
hgrid2.layout()
if with_print:
# save as file
fig.plot()
fig.print2file('./tuto005.png')
return(fig)
if __name__ == '__main__':
script(True)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment