Commit 65e6a085 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Made ratio becoming optional

I set ratio to be a explicit optional argument. May not be the correct way... need to test.
An unexplicit argument may be better for user.

I also add a function in myAxes computeSmartRect

the name of the function is not explicit and may be changed, the question is also if it should be
included within the class or not...
parent 1b4c9bf4
......@@ -49,18 +49,62 @@ from . import Axes
from . import rcParams
# COMPUTE SMART FRAME -----------------------------------------------
def computeSmartRect(fig, ratio, frameRect):
# 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]
return(rect)
# Class MyAxes Overwriting Matplotlib.figure.Axes
class MyAxes(Axes):
# CONSTRUCTOR --------------------------------------------------------
def __init__(self, fig, ratio, frameRect, *args, **kwargs):
def __init__(self, fig, frameRect, ratio=None, *args, **kwargs):
self.fig = fig
self.declareKeywords()
rect = self.computeRect(ratio, frameRect, *args, **kwargs)
rect = self.computeRect(frameRect, ratio=ratio, *args, **kwargs)
# clean kwargs
kwargs.pop('forceRatioOnWidth', None)
kwargs.pop('forceRatioOnHeight', None)
kwargs.pop('fitInFrame', None)
# parent constructor
Axes.__init__(self, fig, rect, **kwargs)
......@@ -68,13 +112,13 @@ class MyAxes(Axes):
# set a default name. Should be individualized by instance
self.name = 'default'
# COMPUTE RECT -------------------------------------------------------
def computeRect(self, ratio, frameRect, *args, **kwargs):
def computeRect(self, frameRect, ratio=None, *args, **kwargs):
# Get the optional keyword
forceRatioOnWidth = kwargs.pop('forceRatioOnWidth', None)
forceRatioOnHeight = kwargs.pop('forceRatioOnHeight', None)
fitInFrame = kwargs.pop('fitInFrame', None)
# get the frame allowed
framePosX, framePosY, frameWidth, frameHeight = frameRect
......@@ -83,13 +127,24 @@ class MyAxes(Axes):
figWidth = self.fig.get_figwidth()
figHeight = self.fig.get_figheight()
if(forceRatioOnWidth):
frameWidth = ratio * figWidth / (frameHeight * figHeight)
elif(forceRatioOnHeight):
frameHeight = ratio * (frameWidth * figWidth) / figHeight
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
rect = [framePosX, framePosY, frameWidth, frameHeight]
if (fitInFrame):
if ratio is None:
ratio = rect[3]/rect[2]
rect = computeSmartRect(self, ratio, rect)
return(rect)
# DECLARE KEYWORDS -------------------------------------------------
......
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