8000 Reuse scale from sharing axis when calling cla(). (#12831) · matplotlib/matplotlib@0f03057 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f03057

Browse files
anntzertimhoffm
authored andcommitted
Reuse scale from sharing axis when calling cla(). (#12831)
The previous code, which called mscale.scale_factory(self._sharex.xaxis.get_scale(), self.xaxis) assumed that this would be sufficient to "copy" a scale object, but in fact this fails to copy e.g. the base of log scales, so the log base would be reset to its default (=10). Instead, just share the scale object, as is done when *creating* a shared axis. This implies that Scales should be reusable across multiple Axis, hence the added note in the docstring. (Note that right now, LogScale objects use the info of whether the axis is an `x` or a `y` axis to decide whether to request arguments named `basex` or `basey`; it would not be too difficult to change the signature of LogScale to always take a `base` argument instead (which would be a more reasonable signature too...); the main problem would be the API difference with `loglog(..., basex=..., basey=...)` -- but on the other hand it's not as if Scales were commonly instantiated by end users (for example one cannot even pass a Scale object to `set_scale`(!))).
1 parent da8dea2 commit 0f03057

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

examples/scales/custom_scale.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ class MercatorLatitudeScale(mscale.ScaleBase):
3838
http://en.wikipedia.org/wiki/Mercator_projection
3939
"""
4040

41-
# The scale class must have a member ``name`` that defines the
42-
# string used to select the scale. For example,
43-
# ``gca().set_yscale("mercator")`` would be used to select this
44-
# scale.
41+
# The scale class must have a member ``name`` that defines the string used
42+
# to select the scale. For example, ``gca().set_yscale("mercator")`` would
43+
# be used to select this scale.
4544
name = 'mercator'
4645

4746
def __init__(self, axis, *, thresh=np.deg2rad(85), **kwargs):
4847
"""
49-
Any keyword arguments passed to ``set_xscale`` and
50-
``set_yscale`` will be passed along to the scale's
51-
constructor.
48+
Any keyword arguments passed to ``set_xscale`` and ``set_yscale`` will
49+
be passed along to the scale's constructor.
5250
5351
thresh: The degree above which to crop the data.
5452
"""
55-
mscale.ScaleBase.__init__(self)
53+
super().__init__(axis)
5654
if thresh >= np.pi / 2:
5755
raise ValueError("thresh must be less than pi/2")
5856
self.thresh = thresh

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,7 @@ def cla(self):
10011001
x0, x1 = self._sharex.get_xlim()
10021002
self.set_xlim(x0, x1, emit=False,
10031003
auto=self._sharex.get_autoscalex_on())
1004-
self.xaxis._scale = mscale.scale_factory(
1005-
self._sharex.xaxis.get_scale(), self.xaxis)
1004+
self.xaxis._scale = self._sharex.xaxis._scale
10061005
else:
10071006
self.xaxis._set_scale('linear')
10081007
try:
@@ -1016,8 +1015,7 @@ def cla(self):
10161015
y0, y1 = self._sharey.get_ylim()
10171016
self.set_ylim(y0, y1, emit=False,
10181017
auto=self._sharey.get_autoscaley_on())
1019-
self.yaxis._scale = mscale.scale_factory(
1020-
self._sharey.yaxis.get_scale(), self.yaxis)
1018+
self.yaxis._scale = self._sharey.yaxis._scale
10211019
else:
10221020
self.yaxis._set_scale('linear')
10231021
try:

lib/matplotlib/scale.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ class ScaleBase(object):
2424
And optionally:
2525
- :meth:`limit_range_for_scale`
2626
"""
27+
28+
def __init__(self, axis, **kwargs):
29+
r"""
30+
Construct a new scale.
31+
32+
Note
33+
----
34+
The following note is for scale implementors.
35+
36+
For back-compatibility reasons, scales take an `~.Axis` object as first
37+
argument. However, this argument should not be used: a single scale
38+
object should be usable by multiple `~.Axis`\es at the same time.
39+
"""
40+
2741
def get_transform(self):
2842
"""
2943
Return the :class:`~matplotlib.transforms.Transform` object
@@ -57,9 +71,6 @@ class LinearScale(ScaleBase):
5771

5872
name = 'linear'
5973

60-
def __init__(self, axis, **kwargs):
61-
pass
62-
6374
def set_default_locators_and_formatters(self, axis):
6475
"""
6576
Set the locators and formatters to reasonable defaults for

0 commit comments

Comments
 (0)
0