You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()
0 commit comments