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

Skip to content

Commit ba7fbb0

Browse files
committed
DOC: add documentation
1 parent 904f247 commit ba7fbb0

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

examples/scales/custom_scale.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
66
Create a custom scale, by implementing the scaling use for latitude data in a
77
Mercator Projection.
8+
9+
Unless you are maiking special use of the `~.Transform` class, you probably
10+
don't need to use this verbose method, but rather instead can use
11+
`~.matplotlib.scale.ArbitraryScale` and the ``'arbitrary'`` option of
12+
`~.matplotlib.axes.Axes.set_xscale` and `~.matplotlib.axes.Axes.set_yscale`.
13+
See the last example in :doc:`/gallery/scales/scales`.
814
"""
915

1016
import numpy as np

examples/scales/scales.py

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
======
55
66
Illustrate the scale transformations applied to axes, e.g. log, symlog, logit.
7+
8+
The last two examples are examples of using the ``'arbitrary'`` scale by
9+
supplying forward and inverse functions for the scale transformation.
710
"""
811
import numpy as np
912
import matplotlib.pyplot as plt
10-
from matplotlib.ticker import NullFormatter
13+
from matplotlib.ticker import NullFormatter, FixedLocator
1114

1215
# Fixing random state for reproducibility
1316
np.random.seed(19680801)
@@ -19,8 +22,8 @@
1922
x = np.arange(len(y))
2023

2124
# plot with various axes scales
22-
fig, axs = plt.subplots(2, 2, sharex=True)
23-
fig.subplots_adjust(left=0.08, right=0.98, wspace=0.3)
25+
fig, axs = plt.subplots(3, 2, figsize=(6, 8),
26+
constrained_layout=True)
2427

2528
# linear
2629
ax = axs[0, 0]
@@ -53,5 +56,73 @@
5356
ax.grid(True)
5457
ax.yaxis.set_minor_formatter(NullFormatter())
5558

59+
# Arbitrary x**(1/2)
60+
def forward(x):
61+
return x**(1/2)
62+
63+
64+
def inverse(x):
65+
return x**2
66+
67+
68+
ax = axs[2, 0]
69+
ax.plot(x, y)
70+
ax.set_yscale('arbitrary', functions=(forward, inverse))
71+
ax.set_title('arbitrary: $x^{1/2}$')
72+
ax.grid(True)
73+
ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 1, 0.2)**2))
74+
ax.yaxis.set_major_locator(FixedLocator(np.arange(0, 1, 0.2)))
75+
76+
77+
# Arbitrary Mercator transform
78+
def forward(a):
79+
a = np.deg2rad(a)
80+
# mask values too close to +/- 90, which are ill-defined.
81+
masked = np.ma.masked_where((a <= -np.pi/2 + 0.02) |
82+
(a >= np.pi/2 - 0.02), a)
83+
if masked.mask.any():
84+
return np.rad2deg(
85+
np.ma.log(np.abs(np.ma.tan(masked) + 1.0 / np.ma.cos(masked))))
86+
else:
87+
return np.rad2deg(np.log(np.abs(np.tan(a) + 1.0 / np.cos(a))))
88+
89+
90+
def inverse(a):
91+
a = np.deg2rad(a)
92+
return np.rad2deg(np.arctan(np.sinh(a)))
93+
94+
ax = axs[2, 1]
95+
96+
t = np.arange(-170.0, 170.0, 0.1)
97+
s = t / 2.
98+
99+
ax.plot(t, s, '-', lw=2)
100+
101+
ax.set_yscale('arbitrary', functions=(forward, inverse))
102+
ax.set_title('arbitrary: Mercator')
103+
ax.grid(True)
104+
ax.set_xlim([-180, 180])
105+
ax.yaxis.set_minor_formatter(NullFormatter())
106+
ax.yaxis.set_major_locator(FixedLocator(np.arange(-90, 90, 30)))
56107

57108
plt.show()
109+
110+
#############################################################################
111+
#
112+
# ------------
113+
#
114+
# References
115+
# """"""""""
116+
#
117+
# The use of the following functions, methods, classes and modules is shown
118+
# in this example:
119+
120+
import matplotlib
121+
matplotlib.axes.Axes.set_yscale
122+
matplotlib.axes.Axes.set_xscale
123+
matplotlib.axis.set_major_locator
124+
matplotlib.scale.Logit
125+
matplotlib.scale.LogScale
126+
matplotlib.scale.LinearScale
127+
matplotlib.scale.SymmetricalLogScale
128+
matplotlib.scale.ArbitraryScale

lib/matplotlib/scale.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ def __init__(self, axis, functions, *,
138138
functions: (forward, inverse)
139139
two-tuple of the forward and inverse functions for the scale.
140140
141+
major_locator: `~matplotlib.ticker.Locator`
142+
optional, allows passing major locator for the axis. Defaults to
143+
`~matplotlib.ticker.AutoLocator`
144+
145+
minor_locator: `~matplotlib.ticker.Locator`
146+
optional, allows passing major locator for the axis. Defaults to
147+
`~matplotlib.ticker.NullLocator`
148+
149+
major_formatter: `~matplotlib.ticker.Formatter`
150+
optional, allows passing major formatter for the axis. Defaults to
151+
`~matplotlib.ticker.ScalarFormatter`
152+
153+
minor_formatter: `~matplotlib.ticker.Formatter`
154+
optional, allows passing maino formatter for the axis. Defaults to
155+
`~matplotlib.ticker.NullFormatter`
156+
157+
Examples
158+
--------
159+
160+
:doc:`/gallery/scales/scales`
161+
162+
163+
141164
"""
142165

143166
forward, inverse = functions

lib/matplotlib/tests/test_scale.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ def test_invalid_log_lims():
155155
def inverse(x):
156156
return x**2
157157

158+
158159
def forward(x):
159160
good = x >= 0
160161
y = np.full_like(x, np.NaN)
161162
y[good] = x[good]**(1/2)
162163
return y
163164

165+
164166
@image_comparison(baseline_images=['arbitrary_scales'], remove_text=True,
165167
extensions=['png'], style='mpl20')
166168
def test_arbitrary_scale():
@@ -170,11 +172,11 @@ def test_arbitrary_scale():
170172

171173
ax.plot(x, x)
172174
ax.set_xscale('arbitrary', functions=(forward, inverse))
173-
ax.set_xlim(1,1000)
175+
ax.set_xlim(1, 1000)
174176

175177

176-
@image_comparison(baseline_images=['arbitrary_scales_passloc'], remove_text=True,
177-
extensions=['png'], style='mpl20')
178+
@image_comparison(baseline_images=['arbitrary_scales_passloc'],
179+
remove_text=True, extensions=['png'], style='mpl20')
178180
def test_arbitrary_scale_passloc():
179181
fig, ax = plt.subplots()
180182

@@ -184,4 +186,4 @@ def test_arbitrary_scale_passloc():
184186
ax.set_xscale('arbitrary', functions=(forward, inverse),
185187
major_locator=mticker.LogLocator(),
186188
minor_locator=mticker.LogLocator(subs=[2, 3, 4, 5, 6, 7, 8, 9]))
187-
ax.set_xlim(1,1000)
189+
ax.set_xlim(1, 1000)

0 commit comments

Comments
 (0)