Commit 5ca9137
committed
Improve handling of degenerate jacobians in non-rectilinear grids.
grid_helper_curvelinear and floating_axes have code to specifically
handle the case where the transform from rectlinear to non-rectilinear
axes has null derivatives in one of the directions, inferring the angle
of the jacobian from the derivative in the other direction. (This angle
defines the rotation applied to axis labels, ticks, and tick labels.)
This approach, however, is insufficient if the derivatives in both
directions are zero. A classical example is e.g. the ``exp(-1/x**2)``
transform, for which all derivatives are zero. To handle this case more
robustly (and also to better encapsulate the angle calculation, which is
currently repeated at a few places), instead, one can increase the step
size of the numerical differentiation until the gradient becomes
nonzero. This amounts to moving along the corresponding gridline until
one actually leaves the position of the tick, and thus is indeed a
justifiable approach to compute the tick rotation.
Full examples:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.projections.polar import PolarTransform
from matplotlib.transforms import Affine2D
from mpl_toolkits.axisartist import (
angle_helper, GridHelperCurveLinear, HostAxes)
import mpl_toolkits.axisartist.floating_axes as floating_axes
# def tr(x, y): return x - y, x + y
# def inv_tr(u, v): return (u + v) / 2, (v - u) / 2
@np.errstate(divide="ignore") # at x=0, exp(-1/x**2)=0; div-by-zero can be ignored.
def tr(x, y):
return np.exp(-x**-2) - np.exp(-y**-2), np.exp(-x**-2) + np.exp(-y**-2)
def inv_tr(u, v):
return (-np.log((u+v)/2))**(1/2), (-np.log((v-u)/2))**(1/2)
plt.subplot(
121, axes_class=floating_axes.FloatingAxes,
grid_helper=floating_axes.GridHelperCurveLinear(
(tr, inv_tr), extremes=(0, 10, 0, 10)))
ax = plt.subplot(
122, axes_class=HostAxes,
grid_helper=GridHelperCurveLinear(
Affine2D().scale(np.pi / 180, 1)
+ PolarTransform()
+ Affine2D().scale(2, 1),
extreme_finder=angle_helper.ExtremeFinderCycle(
20, 20,
lon_cycle=360, lat_cycle=None,
lon_minmax=None, lat_minmax=(0, np.inf),
),
grid_locator1=angle_helper.LocatorDMS(12),
tick_formatter1=angle_helper.FormatterDMS(),
),
aspect=1, xlim=(-5, 12), ylim=(-5, 10))
ax.axis["lat"] = axis = ax.new_floating_axis(0, 40)
axis.label.set_text(r"$\theta = 40^{\circ}$")
axis.label.set_visible(True)
ax.grid(True)
plt.show()1 parent 07002c2 commit 5ca9137
File tree
3 files changed
+102
-41
lines changed- lib/mpl_toolkits/axisartist
- tests
3 files changed
+102
-41
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
75 | | - | |
| 74 | + | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
| 81 | + | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | 87 | | |
94 | 88 | | |
95 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
24 | 47 | | |
25 | | - | |
| 48 | + | |
| 49 | + | |
26 | 50 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
42 | 88 | | |
43 | 89 | | |
44 | 90 | | |
| |||
167 | 213 | | |
168 | 214 | | |
169 | 215 | | |
170 | | - | |
| 216 | + | |
171 | 217 | | |
172 | 218 | | |
173 | 219 | | |
174 | | - | |
175 | | - | |
| 220 | + | |
176 | 221 | | |
177 | 222 | | |
178 | 223 | | |
| |||
197 | 242 | | |
198 | 243 | | |
199 | 244 | | |
200 | | - | |
| 245 | + | |
201 | 246 | | |
202 | 247 | | |
203 | 248 | | |
204 | 249 | | |
205 | 250 | | |
206 | | - | |
| 251 | + | |
207 | 252 | | |
208 | 253 | | |
209 | 254 | | |
210 | 255 | | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | 256 | | |
218 | 257 | | |
219 | 258 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
| |||
113 | 115 | | |
114 | 116 | | |
115 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
0 commit comments