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

Skip to content

Commit bcee870

Browse files
committed
Reuse scale from sharing axis when calling cla().
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 cbd5de9 commit bcee870

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

examples/scales/custom_scale.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,18 @@ 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)
5653
if thresh >= np.pi / 2:
5754
raise ValueError("thresh must be less than pi/2")
5855
self.thresh = thresh

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,7 @@ def cla(self):
969969
x0, x1 = self._sharex.get_xlim()
970970
self.set_xlim(x0, x1, emit=False,
971971
auto=self._sharex.get_autoscalex_on())
972-
self.xaxis._scale = mscale.scale_factory(
973-
self._sharex.xaxis.get_scale(), self.xaxis)
972+
self.xaxis._scale = self._sharex.xaxis._scale
974973
else:
975974
self.xaxis._set_scale('linear')
976975
try:
@@ -984,8 +983,7 @@ def cla(self):
984983
y0, y1 = self._sharey.get_ylim()
985984
self.set_ylim(y0, y1, emit=False,
986985
auto=self._sharey.get_autoscaley_on())
987-
self.yaxis._scale = mscale.scale_factory(
988-
self._sharey.yaxis.get_scale(), self.yaxis)
986+
self.yaxis._scale = self._sharey.yaxis._scale
989987
else:
990988
self.yaxis._set_scale('linear')
991989
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