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

Skip to content

Ability to scale axis by a fixed factor #10321

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

Merged
merged 14 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions doc/users/next_whats_new/2018_01_25_scale_axis_by_factor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Ability to scale axis by a fixed order of magnitude
---------------------------------------------------

To scale an axis by a fixed order of magnitude, set the *scilimits* argument of
``Axes.ticklabel_format`` to the same (non-zero) lower and upper limits. Say to scale
the y axis by a million (1e6), use ``ax.ticklabel_format(style='sci', scilimits=(6, 6), axis='y')``.

The behavior of ``scilimits=(0, 0)`` is unchanged. With this setting, matplotlib will adjust
the order of magnitude depending on the axis values, rather than keeping it fixed. Previously, setting
``scilimits=(m, m)`` was equivalent to setting ``scilimits=(0, 0)``.
2 changes: 2 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2672,6 +2672,8 @@ def ticklabel_format(self, **kwargs):
be used for numbers outside the range
10`m`:sup: to 10`n`:sup:.
Use (0,0) to include all numbers.
Use (m,m) where m <> 0 to fix the order
of magnitude to 10`m`:sup:.
*useOffset* [ bool | offset ]; if True,
the offset will be calculated as needed;
if False, no offset will be used; if a
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ class TestScalarFormatter(object):

use_offset_data = [True, False]

scilimits_data = [
(False, (0, 0), (10.0, 20.0), 0),
(True, (-2, 2), (-10, 20), 0),
(True, (-2, 2), (-20, 10), 0),
(True, (-2, 2), (-110, 120), 2),
(True, (-2, 2), (-120, 110), 2),
(True, (-2, 2), (-.001, 0.002), -3),
(True, (0, 0), (-1e5, 1e5), 5),
(True, (6, 6), (-1e5, 1e5), 6),
]
Copy link
Member

Choose a reason for hiding this comment

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

can you add a scalar example into here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we've decided not to take in scalar anymore, this is a moot point..., but just for posterity sake, my intention was to allow scalar input at the ticklabel_format API level, not at this lower level, where the test sits. The scalar input would then be translated to pair of (repeated) values for lower level consumption.


@pytest.mark.parametrize('left, right, offset', offset_data)
def test_offset_value(self, left, right, offset):
fig, ax = plt.subplots()
Expand Down Expand Up @@ -257,6 +268,18 @@ def test_use_offset(self, use_offset):
tmp_form = mticker.ScalarFormatter()
assert use_offset == tmp_form.get_useOffset()

@pytest.mark.parametrize(
'sci_type, scilimits, lim, orderOfMag', scilimits_data)
def test_scilimits(self, sci_type, scilimits, lim, orderOfMag):
tmp_form = mticker.ScalarFormatter()
tmp_form.set_scientific(sci_type)
tmp_form.set_powerlimits(scilimits)
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(tmp_form)
ax.set_ylim(*lim)
tmp_form.set_locs(ax.yaxis.get_majorticklocs())
assert orderOfMag == tmp_form.orderOfMagnitude


class FakeAxis(object):
"""Allow Formatter to be called without having a "full" plot set up."""
Expand Down
6 changes: 5 additions & 1 deletion lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,14 @@ def _compute_offset(self):
def _set_orderOfMagnitude(self, range):
# if scientific notation is to be used, find the appropriate exponent
# if using an numerical offset, find the exponent after applying the
# offset
# offset. When lower power limit = upper <> 0, use provided exponent.
if not self._scientific:
self.orderOfMagnitude = 0
return
if self._powerlimits[0] == self._powerlimits[1] != 0:
# fixed scaling when lower power limit = upper <> 0.
self.orderOfMagnitude = self._powerlimits[0]
return
locs = np.abs(self.locs)
if self.offset:
oom = math.floor(math.log10(range))
Expand Down