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

Skip to content

Commit 5205ba3

Browse files
committed
Made it possible to pickle Log scale plots.
1 parent d94036c commit 5205ba3

File tree

2 files changed

+64
-44
lines changed

2 files changed

+64
-44
lines changed

lib/matplotlib/scale.py

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,7 @@ def _clip_non_positives(a):
9898
return a
9999

100100

101-
class LogScale(ScaleBase):
102-
"""
103-
A standard logarithmic scale. Care is taken so non-positive
104-
values are not plotted.
105-
106-
For computational efficiency (to push as much as possible to Numpy
107-
C code in the common cases), this scale provides different
108-
transforms depending on the base of the logarithm:
109-
110-
- base 10 (:class:`Log10Transform`)
111-
- base 2 (:class:`Log2Transform`)
112-
- base e (:class:`NaturalLogTransform`)
113-
- arbitrary base (:class:`LogTransform`)
114-
"""
115-
116-
name = 'log'
117-
101+
if True:
118102
class LogTransformBase(Transform):
119103
input_dims = 1
120104
output_dims = 1
@@ -138,7 +122,7 @@ def transform_non_affine(self, a):
138122
return np.log10(a)
139123

140124
def inverted(self):
141-
return LogScale.InvertedLog10Transform()
125+
return InvertedLog10Transform()
142126

143127
class InvertedLog10Transform(Transform):
144128
input_dims = 1
@@ -151,7 +135,7 @@ def transform_non_affine(self, a):
151135
return ma.power(10.0, a) / 10.0
152136

153137
def inverted(self):
154-
return LogScale.Log10Transform()
138+
return Log10Transform()
155139

156140
class Log2Transform(LogTransformBase):
157141
base = 2.0
@@ -163,7 +147,7 @@ def transform_non_affine(self, a):
163147
return np.log2(a)
164148

165149
def inverted(self):
166-
return LogScale.InvertedLog2Transform()
150+
return InvertedLog2Transform()
167151

168152
class InvertedLog2Transform(Transform):
169153
input_dims = 1
@@ -176,7 +160,7 @@ def transform_non_affine(self, a):
176160
return ma.power(2.0, a) / 2.0
177161

178162
def inverted(self):
179-
return LogScale.Log2Transform()
163+
return Log2Transform()
180164

181165
class NaturalLogTransform(LogTransformBase):
182166
base = np.e
@@ -188,7 +172,7 @@ def transform_non_affine(self, a):
188172
return np.log(a)
189173

190174
def inverted(self):
191-
return LogScale.InvertedNaturalLogTransform()
175+
return InvertedNaturalLogTransform()
192176

193177
class InvertedNaturalLogTransform(Transform):
194178
input_dims = 1
@@ -201,7 +185,7 @@ def transform_non_affine(self, a):
201185
return ma.power(np.e, a) / np.e
202186

203187
def inverted(self):
204-
return LogScale.NaturalLogTransform()
188+
return NaturalLogTransform()
205189

206190
class LogTransform(Transform):
207191
input_dims = 1
@@ -224,7 +208,7 @@ def transform_non_affine(self, a):
224208
return np.log(a) / np.log(self.base)
225209

226210
def inverted(self):
227-
return LogScale.InvertedLogTransform(self.base)
211+
return InvertedLogTransform(self.base)
228212

229213
class InvertedLogTransform(Transform):
230214
input_dims = 1
@@ -240,7 +224,35 @@ def transform_non_affine(self, a):
240224
return ma.power(self.base, a) / self.base
241225

242226
def inverted(self):
243-
return LogScale.LogTransform(self.base)
227+
return LogTransform(self.base)
228+
229+
230+
class LogScale(ScaleBase):
231+
"""
232+
A standard logarithmic scale. Care is taken so non-positive
233+
values are not plotted.
234+
235+
For computational efficiency (to push as much as possible to Numpy
236+
C code in the common cases), this scale provides different
237+
transforms depending on the base of the logarithm:
238+
239+
- base 10 (:class:`Log10Transform`)
240+
- base 2 (:class:`Log2Transform`)
241+
- base e (:class:`NaturalLogTransform`)
242+
- arbitrary base (:class:`LogTransform`)
243+
"""
244+
name = 'log'
245+
246+
# compatibility shim
247+
LogTransformBase = LogTransformBase
248+
Log10Transform = Log10Transform
249+
InvertedLog10Transform = InvertedLog10Transform
250+
Log2Transform = Log2Transform
251+
InvertedLog2Transform = InvertedLog2Transform
252+
NaturalLogTransform = NaturalLogTransform
253+
InvertedNaturalLogTransform = InvertedNaturalLogTransform
254+
LogTransform = LogTransform
255+
InvertedLogTransform = InvertedLogTransform
244256

245257
def __init__(self, axis, **kwargs):
246258
"""
@@ -308,18 +320,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
308320
vmax <= 0.0 and minpos or vmax)
309321

310322

311-
class SymmetricalLogScale(ScaleBase):
312-
"""
313-
The symmetrical logarithmic scale is logarithmic in both the
314-
positive and negative directions from the origin.
315-
316-
Since the values close to zero tend toward infinity, there is a
317-
need to have a range around zero that is linear. The parameter
318-
*linthresh* allows the user to specify the size of this range
319-
(-*linthresh*, *linthresh*).
320-
"""
321-
name = 'symlog'
322-
323+
if True:
323324
class SymmetricalLogTransform(Transform):
324325
input_dims = 1
325326
output_dims = 1
@@ -349,8 +350,8 @@ def transform_non_affine(self, a):
349350
return log
350351

351352
def inverted(self):
352-
return SymmetricalLogScale.InvertedSymmetricalLogTransform(
353-
self.base, self.linthresh, self.linscale)
353+
return InvertedSymmetricalLogTransform(self.base, self.linthresh,
354+
self.linscale)
354355

355356
class InvertedSymmetricalLogTransform(Transform):
356357
input_dims = 1
@@ -360,9 +361,7 @@ class InvertedSymmetricalLogTransform(Transform):
360361

361362
def __init__(self, base, linthresh, linscale):
362363
Transform.__init__(self)
363-
symlog = SymmetricalLogScale.SymmetricalLogTransform(base,
364-
linthresh,
365-
linscale)
364+
symlog = SymmetricalLogTransform(base, linthresh, linscale)
366365
self.base = base
367366
self.linthresh = linthresh
368367
self.invlinthresh = symlog.transform(linthresh)
@@ -382,8 +381,23 @@ def transform_non_affine(self, a):
382381
return exp
383382

384383
def inverted(self):
385-
return SymmetricalLogScale.SymmetricalLogTransform(
386-
self.base, self.linthresh, self.linscale)
384+
return SymmetricalLogTransform(self.base,
385+
self.linthresh, self.linscale)
386+
387+
class SymmetricalLogScale(ScaleBase):
388+
"""
389+
The symmetrical logarithmic scale is logarithmic in both the
390+
positive and negative directions from the origin.
391+
392+
Since the values close to zero tend toward infinity, there is a
393+
need to have a range around zero that is linear. The parameter
394+
*linthresh* allows the user to specify the size of this range
395+
(-*linthresh*, *linthresh*).
396+
"""
397+
name = 'symlog'
398+
# compatibility shim
399+
SymmetricalLogTransform = SymmetricalLogTransform
400+
InvertedSymmetricalLogTransform = InvertedSymmetricalLogTransform
387401

388402
def __init__(self, axis, **kwargs):
389403
"""

lib/matplotlib/tests/test_pickle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def test_simple():
115115
# recursive_pickle(ax, 'figure')
116116
# pickle.dump(ax, BytesIO(), pickle.HIGHEST_PROTOCOL)
117117

118+
fig = plt.figure()
119+
ax = plt.axes()
120+
plt.plot(range(10))
121+
ax.set_yscale('log')
122+
pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)
123+
118124

119125
@image_comparison(baseline_images=['multi_pickle'],
120126
extensions=['png'], remove_text=True)

0 commit comments

Comments
 (0)