Skip to content

Commit b7af6a7

Browse files
committed
Fix polar coordinate type checking
1 parent e52c341 commit b7af6a7

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

plotnine/coords/coord_polar.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import replace
4-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, cast
55

66
import numpy as np
77

@@ -11,6 +11,7 @@
1111
if TYPE_CHECKING:
1212
import pandas as pd
1313
from matplotlib.axes import Axes
14+
from matplotlib.projections.polar import PolarAxes
1415

1516
from plotnine.iapi import panel_view
1617
from plotnine.scales.scale import scale
@@ -216,8 +217,8 @@ def backtransform_range(self, panel_params: panel_view) -> panel_ranges:
216217
t_range = tuple(self.params["theta_range"])
217218
r_range = tuple(self.params["r_range"])
218219
if self.theta == "x":
219-
return panel_ranges(x=t_range, y=r_range) # type: ignore[arg-type]
220-
return panel_ranges(x=r_range, y=t_range) # type: ignore[arg-type]
220+
return panel_ranges(x=t_range, y=r_range)
221+
return panel_ranges(x=r_range, y=t_range)
221222

222223
# ------------------------------------------------------------------
223224
# Draw decorations on PolarAxes
@@ -231,10 +232,11 @@ def draw(self, axs: list[Axes]) -> None:
231232
mpl_direction = -1 if self.direction == 1 else 1
232233

233234
for ax in axs:
234-
ax.set_theta_zero_location("N") # 12 o'clock = 0
235-
ax.set_theta_direction(mpl_direction)
235+
polar_ax = cast("PolarAxes", ax)
236+
polar_ax.set_theta_zero_location("N") # 12 o'clock = 0
237+
polar_ax.set_theta_direction(mpl_direction)
236238
if np.isfinite(r_min) and np.isfinite(r_max) and r_min != r_max:
237-
ax.set_rlim(float(r_min), float(r_max))
239+
polar_ax.set_rlim(float(r_min), float(r_max))
238240

239241
# ------------------------------------------------------------------
240242
# Misc

plotnine/coords/coord_radial.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import replace
4-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, Sequence, cast
55

66
import numpy as np
77

@@ -10,6 +10,7 @@
1010
if TYPE_CHECKING:
1111
import pandas as pd
1212
from matplotlib.axes import Axes
13+
from matplotlib.projections.polar import PolarAxes
1314

1415
from plotnine.iapi import panel_view
1516
from plotnine.scales.scale import scale
@@ -184,7 +185,8 @@ def setup_panel_params(self, scale_x: scale, scale_y: scale) -> panel_view:
184185
if self.rlim is not None:
185186
self.params["r_range"] = tuple(self.rlim)
186187
rlo, rhi = self.rlim
187-
breaks, labels = pv.y.breaks, pv.y.labels
188+
breaks = cast("Sequence[float]", pv.y.breaks)
189+
labels = pv.y.labels
188190
mask = [rlo <= b <= rhi for b in breaks]
189191
new_y = replace(
190192
pv.y,
@@ -287,11 +289,12 @@ def draw(self, axs: list[Axes]) -> None:
287289
arc = self._arc
288290

289291
for ax in axs:
292+
polar_ax = cast("PolarAxes", ax)
290293
# Restrict visible theta range for partial arcs.
291294
if self.end is not None:
292295
theta_lo = min(self.start, self.start + arc)
293296
theta_hi = max(self.start, self.start + arc)
294-
ax.set_thetalim(theta_lo, theta_hi)
297+
polar_ax.set_thetalim(theta_lo, theta_hi)
295298

296299
# Inner radius: push the data away from the centre by setting a
297300
# virtual r-origin below r_min. Formula: solve
@@ -306,16 +309,18 @@ def draw(self, axs: list[Axes]) -> None:
306309
r_origin = (r_min - self.inner_radius * r_max) / (
307310
1.0 - self.inner_radius
308311
)
309-
ax.set_rorigin(r_origin)
312+
polar_ax.set_rorigin(r_origin)
310313

311314
# Radial axis label placement.
312315
if self.r_axis_inside is not None:
313316
if isinstance(self.r_axis_inside, bool):
314317
if self.r_axis_inside:
315318
# Just inside the start angle keeps it out of the data.
316-
ax.set_rlabel_position(np.degrees(self.start) + 10)
319+
polar_ax.set_rlabel_position(
320+
np.degrees(self.start) + 10
321+
)
317322
else:
318-
ax.set_rlabel_position(
323+
polar_ax.set_rlabel_position(
319324
np.degrees(float(self.r_axis_inside))
320325
)
321326

0 commit comments

Comments
 (0)