Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix polar transform with non-linear scales
  • Loading branch information
dstansby committed Jan 3, 2023
commit f063fd27f116887e5a12f3782bed54770f6e4b2e
13 changes: 9 additions & 4 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PolarTransform(mtransforms.Transform):
input_dims = output_dims = 2

def __init__(self, axis=None, use_rmin=True,
_apply_theta_transforms=True):
_apply_theta_transforms=True, *, scale_transform=None):
"""
Parameters
----------
Expand All @@ -46,12 +46,17 @@ def __init__(self, axis=None, use_rmin=True,
self._axis = axis
self._use_rmin = use_rmin
self._apply_theta_transforms = _apply_theta_transforms
self._scale_transform = scale_transform

__str__ = mtransforms._make_str_method(
"_axis",
use_rmin="_use_rmin",
_apply_theta_transforms="_apply_theta_transforms")

def _get_rorigin(self):
# Get lower r limit after being scaled by the radial scale transform
return self._scale_transform.transform((0, self._axis.get_rorigin()))[1]

def transform_non_affine(self, tr):
# docstring inherited
theta, r = np.transpose(tr)
Expand All @@ -61,7 +66,7 @@ def transform_non_affine(self, tr):
theta *= self._axis.get_theta_direction()
theta += self._axis.get_theta_offset()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a test that uses these lines?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like _apply_theta_transforms=True case is not used internally, but as a backwards compatability wrapper for users who used PolarTransform before its behaviour was changed. I think adding a meaninful test would be tricky here.

if self._use_rmin and self._axis is not None:
r = (r - self._axis.get_rorigin()) * self._axis.get_rsign()
r = (r - self._get_rorigin()) * self._axis.get_rsign()
r = np.where(r >= 0, r, np.nan)
return np.column_stack([r * np.cos(theta), r * np.sin(theta)])

Expand All @@ -85,7 +90,7 @@ def transform_path_non_affine(self, path):
# that behavior here.
last_td, td = np.rad2deg([last_t, t])
if self._use_rmin and self._axis is not None:
r = ((r - self._axis.get_rorigin())
r = ((r - self._get_rorigin())
* self._axis.get_rsign())
if last_td <= td:
while td - last_td > 360:
Expand Down Expand Up @@ -877,7 +882,7 @@ def _set_lim_and_transforms(self):
# data. This one is aware of rmin
self.transProjection = self.PolarTransform(
self,
_apply_theta_transforms=False)
_apply_theta_transforms=False, scale_transform=self.transScale)
# Add dependency on rorigin.
self.transProjection.set_children(self._originViewLim)

Expand Down