Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Made code in ticker.py PEP8 compliant
  • Loading branch information
VincentVandalon committed Mar 2, 2016
commit 5368336d44490d935c803a38c87c1dee502e87e2
156 changes: 70 additions & 86 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ def __call__(self, x, pos=None):
return self.pprint_val(x, d)

def pprint_val(self, x, d):
#if the number is not too big and it's an int, format it as an
#int
# if the number is not too big and it's an int, format it as an
# int
if abs(x) < 1e4 and x == int(x):
return '%d' % x

Expand All @@ -395,7 +395,7 @@ def pprint_val(self, x, d):
else:
fmt = '%1.3f'
s = fmt % x
#print d, x, fmt, s
# print d, x, fmt, s

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove this line.

tup = s.split('e')
if len(tup) == 2:
mantissa = tup[0].rstrip('0').rstrip('.')
Expand All @@ -417,10 +417,7 @@ class ScalarFormatter(Formatter):
axes.formatter.limits rc parameter.

"""
mode = {'offset' :0,
'scaling' :1,
'none' :2,
}
mode = {'offset': 0, 'scaling': 1, 'none': 2, }
_currentMode = 2

def __init__(self, useOffset=None, useMathText=None, useLocale=None):
Expand All @@ -430,9 +427,9 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None):

self.set_useOffset(False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it no longer uses the rcparam?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still honors the rcparam, see line 671 and 672. I (probably) should have been clearer about this in the commit message.

_useoffset and get_useOffset() now indicates if the offset has been set by either the user or the algorithm choosing a good offset; It no longer return the same value as the rcParam.

axes.formatter.useoffset = False now disables the automatic algorithm. The user can still manually set an offset
axes.formatter.useoffset = True allows the automatic algorithm to set an offset. The user can still override this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The practice in the rest of the library is that the value of the rcparams are stashed when the artists are created. In part this is so that the rcparams context manager and in part so that we can be sure of when the rcprams are consulted (many things are deffered to draw time).

self.set_useScalingFactor(False)
if rcParams['axes.formatter.useoffset'] == True:
self._currentMode=self.mode['offset']
self._currentMode=self.mode['none']
if rcParams['axes.formatter.useoffset'] is True:
self._currentMode = self.mode['offset']
self._currentMode = self.mode['none']

self._usetex = rcParams['text.usetex']
if useMathText is None:
Expand All @@ -450,73 +447,73 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None):
def get_useScalingFactor(self):
return self._currentMode == self.mode['scaling']

def set_useScalingFactor(self,val):
def set_useScalingFactor(self, val):
"""
Control and set the scaling factor. Disabling this returns the
Control and set the scaling factor. Disabling this returns the
formatter to its default behavior, e.g. it will try to find
an appropriate scaling/offset if enabled.
Note that either offset or scaling can be used. Therefore,
Note that either offset or scaling can be used. Therefore,
this automatically turns off offset

Parameters
----------
val : (True|False|numeric)
Enable, disable, or sets and enables the use of scaling factor
in the axis.
in the axis.

Returns
-------
NONE
"""

if val in [True, False]:
self.orderOfMagnitude=math.log10(1)
self._currentMode=self.mode['scaling']
if val == False:
self._currentMode=self.mode['none']
elif isinstance(val,numbers.Number):
self._currentMode=self.mode['scaling']
self.orderOfMagnitude=.5*math.log10(val*val)
self.orderOfMagnitude = math.log10(1)
self._currentMode = self.mode['scaling']
if val is False:
self._currentMode = self.mode['none']
elif isinstance(val, numbers.Number):
self._currentMode = self.mode['scaling']
self.orderOfMagnitude = .5*math.log10(val*val)
else:
raise ValueError("'val' must be a number or a boolean")

useScalingFactor = property(fget=get_useScalingFactor, fset=set_useScalingFactor)
useScalingFactor = property(fget=get_useScalingFactor,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if adding new properties can you use use_scaling_factor?

fset=set_useScalingFactor)

def get_useOffset(self):
return self._currentMode == self.mode['offset']

def set_useOffset(self, val):
"""
Control and set the offset. Disabling this returns the
Control and set the offset. Disabling this returns the
formatter to its default behavior, e.g. it will try to find
an appropriate scaling/offset if enabled.
Note that either offset or scaling can be used. Therefore,
Note that either offset or scaling can be used. Therefore,
this automatically turns off scaling

Parameters
----------
val : (True|False|numeric)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val : bool or scaler I think in the right numpydoc way to write this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scalar not scaler.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Enable, disable, or sets and enables the use of an offset
in the axis.
in the axis.

Returns
-------
NONE
"""
if val in [True, False]:
self.offsetval = 0
self._currentMode=self.mode['offset']
if val == False:
self._currentMode=self.mode['none']
elif isinstance(val,numbers.Number):
self._currentMode=self.mode['offset']
self._currentMode = self.mode['offset']
if val is False:
self._currentMode = self.mode['none']
elif isinstance(val, numbers.Number):
self._currentMode = self.mode['offset']
self.offsetval = val
else:
raise ValueError("'val' must be a number or a boolean")

useOffset = property(fget=get_useOffset, fset=set_useOffset)


def get_useLocale(self):
return self._useLocale

Expand Down Expand Up @@ -545,13 +542,13 @@ def __call__(self, x, pos=None):

def set_scientific(self, b):
"""
Enable/disable scientific notation
Enable/disable scientific notation
see also :meth:`set_powerlimits`

Parameters
----------
b : (True|False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just b : bool

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Enable, disable scientific notation
Enable, disable scientific notation

Returns
-------
Expand Down Expand Up @@ -597,12 +594,11 @@ def format_data(self, value):
s = self._formatSciNotation(s)
return self.fix_minus(s)


def set_offset_string(self,s):
def set_offset_string(self, value):
"""
Set the string which typically contains the offset
or the scaling factor which is to be displayed on the axis.
Set this to None to allow the string set by offset or scaling
or the scaling factor which is to be displayed on the axis.
Set this to None to allow the string set by offset or scaling
algorithm.

Parameters
Expand All @@ -613,13 +609,13 @@ def set_offset_string(self,s):
-------
None
"""
self._offsetString=s
self._offsetString = value

def get_offset(self):
"""
Returns a string with the offset(or scientific notation)/scaling
factor which is properly formatted. This is used as additional text
next to the ticks, either determined by offset/scaling or set by the
next to the ticks, either determined by offset/scaling or set by the
user

Parameters

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not include unneeded sections.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if you mean (1) leave it as it was or (2) no documentation needed for this method as a user will probably not use this method. Which one do you prefer?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the docstring is good, just do not include the Parameters section (as there ane no parameters).

Expand All @@ -630,29 +626,30 @@ def get_offset(self):
-------
:string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no : here

"""
#String has been set manually, so just return that
if self._offsetString != None :
# String has been set manually, so just return that
if self._offsetString is not None:
return self._offsetString

if len(self.locs) == 0:
return ''
s = ''
offsetStr = ''
sciNotStr = ''
if self._currentMode == self.mode['offset']:
if self._currentMode == self.mode['offset']:
if self._currentMode == self.mode['offset']:
offsetStr = self.format_data(self.offsetval)
if self.offsetval > 0:
offsetStr = ' +' + offsetStr
elif self._currentMode == self.mode['scaling']:
elif self._currentMode == self.mode['scaling']:
if self.orderOfMagnitude:
if self._usetex or self._useMathText:
sciNotStr = self.format_data(10 ** self.orderOfMagnitude)
else:
#sciNotStr = '%g' % 10 **self.orderOfMagnitude
sciNotStr = '\u22C5 ' + self.format_data(10 ** self.orderOfMagnitude)
# sciNotStr = '%g' % 10 **self.orderOfMagnitude
sciNotStr = ('\u22C5 ' + self.format_data(
10 ** self.orderOfMagnitude))

#Do final formatting
# Do final formatting
if self._useMathText:
if sciNotStr != '':
sciNotStr = r'\times%s' % _mathdefault(sciNotStr)
Expand All @@ -679,10 +676,10 @@ def set_locs(self, locs):
self._set_format(vmin, vmax)

def _set_offset(self, range):
#Determine if an offset is needed and if so, set it.
#This is only needed when scaling/offset hasn't been set by the user
# Determine if an offset is needed and if so, set it.
# This is only needed when scaling/offset hasn't been set by the user
if self._currentMode != self.mode['none']:
return
return

# offset of 20,001 is 20,000, for example
locs = self.locs
Expand All @@ -707,13 +704,12 @@ def _set_offset(self, range):
else:
self.set_useOffset(False)


def _set_orderOfMagnitude(self, range):
# If the user has set scale/offset, their input is used
# if scientific notation is to be used, find the appropriate exponent

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to grouping methods (user, inherited, local), the diff is somewhat hard to read.

# if using an numerical offset, find the exponent after applying the
if self._currentMode != self.mode['none'] or self._scientific == False:
#User specified or unwanted
if self._currentMode != self.mode['none'] or self._scientific is False:
# User specified or unwanted
return

locs = np.absolute(self.locs)
Expand All @@ -730,10 +726,10 @@ def _set_orderOfMagnitude(self, range):
oom = math.floor(math.log10(val))
if oom <= self._powerlimits[0]:
self.orderOfMagnitude = oom
self._currentMode=self.mode['scaling']
self._currentMode = self.mode['scaling']
elif oom >= self._powerlimits[1]:
self.orderOfMagnitude = oom
self._currentMode=self.mode['scaling']
self._currentMode = self.mode['scaling']
else:
self.orderOfMagnitude = 0

Expand All @@ -744,7 +740,8 @@ def _set_format(self, vmin, vmax):
_locs = list(self.locs) + [vmin, vmax]
else:
_locs = self.locs
locs = (np.asarray(_locs) - self.offsetval) / 10. ** self.orderOfMagnitude
locs = ((np.asarray(_locs) - self.offsetval) /
10. ** self.orderOfMagnitude)
loc_range = np.ptp(locs)
# Curvilinear coordinates can yield two identical points.
if loc_range == 0:
Expand Down Expand Up @@ -773,8 +770,8 @@ def _set_format(self, vmin, vmax):
self.format = '$%s$' % _mathdefault(self.format)

def pprint_val(self, x):
#Decide if we are doing offset, scale, or none
if self._currentMode == self.mode['offset'] :
# Decide if we are doing offset, scale, or none
if self._currentMode == self.mode['offset']:
xp = x - self.offsetval
elif self._currentMode == self.mode['scaling']:
xp = x / (10. ** self.orderOfMagnitude)
Expand Down Expand Up @@ -877,8 +874,8 @@ def format_data_short(self, value):
return '%-12g' % value

def pprint_val(self, x, d):
#if the number is not too big and it's an int, format it as an
#int
# if the number is not too big and it's an int, format it as an
# int
if abs(x) < 1e4 and x == int(x):
return '%d' % x

Expand Down Expand Up @@ -1025,25 +1022,10 @@ class EngFormatter(Formatter):
# (https://github.com/jcrocholl/pep8/issues/271)

# The SI engineering prefixes
ENG_PREFIXES = {
-24: "y",
-21: "z",
-18: "a",
-15: "f",
-12: "p",
-9: "n",
-6: "\u03bc",
-3: "m",
0: "",
3: "k",
6: "M",
9: "G",
12: "T",
15: "P",
18: "E",
21: "Z",
24: "Y"
}
ENG_PREFIXES = {-24: "y", -21: "z", -18: "a", -15: "f",
-12: "p", -9: "n", -6: "\u03bc", -3: "m",
0: "", 3: "k", 6: "M", 9: "G", 12: "T",
15: "P", 18: "E", 21: "Z", 24: "Y"}

def __init__(self, unit="", places=None):
self.unit = unit
Expand Down Expand Up @@ -1398,15 +1380,15 @@ def le(self, x):
'return the largest multiple of base <= x'
d, m = divmod(x, self._base)
if closeto(m / self._base, 1): # was closeto(m, self._base)
#looks like floating point error
# looks like floating point error
return (d + 1) * self._base
return d * self._base

def gt(self, x):
'return the smallest multiple of base > x'
d, m = divmod(x, self._base)
if closeto(m / self._base, 1):
#looks like floating point error
# looks like floating point error
return (d + 2) * self._base
return (d + 1) * self._base

Expand Down Expand Up @@ -1605,8 +1587,9 @@ def __call__(self):
return self.tick_values(vmin, vmax)

def tick_values(self, vmin, vmax):
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander=1e-13,
tiny=1e-14)
vmin, vmax = mtransforms.nonsingular(vmin,
vmax, expander=1e-13,
tiny=1e-14)
locs = self.bin_boundaries(vmin, vmax)
prune = self._prune
if prune == 'lower':
Expand All @@ -1624,8 +1607,9 @@ def view_limits(self, dmin, dmax):
dmin = -maxabs
dmax = maxabs

dmin, dmax = mtransforms.nonsingular(dmin, dmax, expander=1e-12,
tiny=1.e-13)
dmin, dmax = mtransforms.nonsingular(dmin,
dmax, expander=1e-12,
tiny=1.e-13)

if rcParams['axes.autolimit_mode'] == 'round_numbers':
return np.take(self.bin_boundaries(dmin, dmax), [0, -1])
Expand Down Expand Up @@ -2180,8 +2164,8 @@ def get_locator(self, d):
fld = math.floor(ld)
base = 10 ** fld

#if ld==fld: base = 10**(fld-1)
#else: base = 10**fld
# if ld==fld: base = 10**(fld-1)
# else: base = 10**fld

if d >= 5 * base:
ticksize = base
Expand Down