-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Offset and scaling factors in axis format #4376 #6086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
VincentVandalon
wants to merge
15
commits into
matplotlib:master
from
VincentVandalon:offsetAxisFormat-4376
Closed
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
137c7aa
Ticket 4376: Implemented functionality to let the user set an offset or
VincentVandalon ab1b3a2
Ticket 4376: Added more information in signature of the functions
VincentVandalon 4079e1a
Reformatted documentation of related function
VincentVandalon bfc7389
Fixed minor issue, default scaling did not use scaling factor
VincentVandalon 09f800c
Added functionality to manually set offset/scaling string
VincentVandalon 02f0a11
Made come more readable and more consistent with matplotlib defaults
VincentVandalon 5368336
Made code in ticker.py PEP8 compliant
VincentVandalon fee8ab9
Removed old test and changed code to respect rcParams
VincentVandalon 681820f
Changes multiplication sign from cdot to cross
VincentVandalon 66061ad
axis.formatter.useoffset is still honored, however, it is not directl…
VincentVandalon 53ed618
Allowed both multiplication and scaling again
VincentVandalon 4c86635
Updated test images to handle addtion of x sign in scaling factor of …
VincentVandalon 9ce9d3c
PEP8 issues
VincentVandalon 8d9d121
Minor improvements suggested by tacaswell
VincentVandalon 2737c5c
- Improved documentation with the suggestions of tacaswell.
VincentVandalon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Made come more readable and more consistent with matplotlib defaults
- Loading branch information
commit 02f0a11397fb89fc57dd904791f616d77ef0b42f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -417,6 +417,11 @@ class ScalarFormatter(Formatter): | |
axes.formatter.limits rc parameter. | ||
|
||
""" | ||
mode = {'offset' :0, | ||
'scaling' :1, | ||
'none' :2, | ||
} | ||
_currentMode = 2 | ||
|
||
def __init__(self, useOffset=None, useMathText=None, useLocale=None): | ||
# useOffset allows plotting small data ranges with large offsets: for | ||
|
@@ -426,8 +431,8 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None): | |
self.set_useOffset(False) | ||
self.set_useScalingFactor(False) | ||
if rcParams['axes.formatter.useoffset'] == True: | ||
self._offsetScaleNone=0 #Basically an enum: 0=offset 1=scale 2=none | ||
self._offsetScaleNone=2 #Basically an enum: 0=offset 1=scale 2=none | ||
self._currentMode=self.mode['offset'] | ||
self._currentMode=self.mode['none'] | ||
|
||
self._usetex = rcParams['text.usetex'] | ||
if useMathText is None: | ||
|
@@ -436,14 +441,14 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None): | |
self.orderOfMagnitude = 0 | ||
self.format = '' | ||
self._scientific = True | ||
self._offsetString = '' | ||
self._offsetString = None | ||
self._powerlimits = rcParams['axes.formatter.limits'] | ||
if useLocale is None: | ||
useLocale = rcParams['axes.formatter.use_locale'] | ||
self._useLocale = useLocale | ||
|
||
def get_useScalingFactor(self): | ||
return self._offsetScaleNone == 1 | ||
return self._currentMode == self.mode['scaling'] | ||
|
||
def set_useScalingFactor(self,val): | ||
""" | ||
|
@@ -466,19 +471,19 @@ def set_useScalingFactor(self,val): | |
|
||
if val in [True, False]: | ||
self.orderOfMagnitude=math.log10(1) | ||
self._offsetScaleNone=1 #0=offset 1=scale 2=none | ||
self._currentMode=self.mode['scaling'] | ||
if val == False: | ||
self._offsetScaleNone=2 #0=offset 1=scale 2=none | ||
self._currentMode=self.mode['none'] | ||
elif isinstance(val,numbers.Number): | ||
self._offsetScaleNone=1 #0=offset 1=scale 2=none | ||
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) | ||
|
||
def get_useOffset(self): | ||
return self._offsetScaleNone == 0 | ||
return self._currentMode == self.mode['offset'] | ||
|
||
def set_useOffset(self, val): | ||
""" | ||
|
@@ -500,11 +505,11 @@ def set_useOffset(self, val): | |
""" | ||
if val in [True, False]: | ||
self.offsetval = 0 | ||
self._offsetScaleNone=0 #0=offset 1=scale 2=none | ||
self._currentMode=self.mode['offset'] | ||
if val == False: | ||
self._offsetScaleNone=2 #0=offset 1=scale 2=none | ||
self._currentMode=self.mode['none'] | ||
elif isinstance(val,numbers.Number): | ||
self._offsetScaleNone=0 #0=offset 1=scale 2=none | ||
self._currentMode=self.mode['offset'] | ||
self.offsetval = val | ||
else: | ||
raise ValueError("'val' must be a number or a boolean") | ||
|
@@ -597,7 +602,7 @@ def set_offset_string(self,s): | |
""" | ||
Set the string which typically contains the offset | ||
or the scaling factor which is to be displayed on the axis. | ||
Set this to and empty string "" allows the string set by offset or scaling | ||
Set this to None to allow the string set by offset or scaling | ||
algorithm. | ||
|
||
Parameters | ||
|
@@ -626,20 +631,20 @@ def get_offset(self): | |
:string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no |
||
""" | ||
#String has been set manually, so just return that | ||
if self._offsetString != '' : | ||
if self._offsetString != None : | ||
return self._offsetString | ||
|
||
if len(self.locs) == 0: | ||
return '' | ||
s = '' | ||
offsetStr = '' | ||
sciNotStr = '' | ||
if self._offsetScaleNone == 0: #OFFSET | ||
if self._offsetScaleNone == 0: | ||
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._offsetScaleNone == 1: #SCALE | ||
elif self._currentMode == self.mode['scaling']: | ||
if self.orderOfMagnitude: | ||
if self._usetex or self._useMathText: | ||
sciNotStr = self.format_data(10 ** self.orderOfMagnitude) | ||
|
@@ -676,7 +681,7 @@ def set_locs(self, locs): | |
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 | ||
if self._offsetScaleNone != 2: | ||
if self._currentMode != self.mode['none']: | ||
return | ||
|
||
# offset of 20,001 is 20,000, for example | ||
|
@@ -707,12 +712,12 @@ 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 | ||
# if using an numerical offset, find the exponent after applying the | ||
if self._offsetScaleNone != 2 or self._scientific == False: | ||
if self._currentMode != self.mode['none'] or self._scientific == False: | ||
#User specified or unwanted | ||
return | ||
|
||
locs = np.absolute(self.locs) | ||
if self._offsetScaleNone == 0: | ||
if self._currentMode == self.mode['offset']: | ||
oom = math.floor(math.log10(range)) | ||
else: | ||
if locs[0] > locs[-1]: | ||
|
@@ -725,10 +730,10 @@ def _set_orderOfMagnitude(self, range): | |
oom = math.floor(math.log10(val)) | ||
if oom <= self._powerlimits[0]: | ||
self.orderOfMagnitude = oom | ||
self._offsetScaleNone=1 #Set to scale | ||
self._currentMode=self.mode['scaling'] | ||
elif oom >= self._powerlimits[1]: | ||
self.orderOfMagnitude = oom | ||
self._offsetScaleNone=1 #Set to scale | ||
self._currentMode=self.mode['scaling'] | ||
else: | ||
self.orderOfMagnitude = 0 | ||
|
||
|
@@ -769,9 +774,9 @@ def _set_format(self, vmin, vmax): | |
|
||
def pprint_val(self, x): | ||
#Decide if we are doing offset, scale, or none | ||
if self._offsetScaleNone == 0 : | ||
if self._currentMode == self.mode['offset'] : | ||
xp = x - self.offsetval | ||
elif self._offsetScaleNone == 1: | ||
elif self._currentMode == self.mode['scaling']: | ||
xp = x / (10. ** self.orderOfMagnitude) | ||
else: | ||
xp = x | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
andget_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 offsetaxes.formatter.useoffset = True
allows the automatic algorithm to set an offset. The user can still override this.There was a problem hiding this comment.
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).