Skip to content

Commit 849820c

Browse files
committed
Transform polar segment endpoints
1 parent 9356b1f commit 849820c

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

plotnine/coords/coord_polar.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,31 @@ def transform(
130130

131131
if self.theta == "x":
132132
theta_col, r_col = "x", "y"
133+
theta_end_col, r_end_col = "xend", "yend"
133134
else:
134135
theta_col, r_col = "y", "x"
136+
theta_end_col, r_end_col = "yend", "xend"
135137

136138
if theta_col not in data.columns or r_col not in data.columns:
137139
return data
138140

139141
data = data.copy()
140142
data[theta_col] = self._to_radians(data[theta_col].to_numpy())
143+
has_endpoints = theta_end_col in data.columns and r_end_col in data.columns
144+
if has_endpoints:
145+
data[theta_end_col] = self._to_radians(
146+
data[theta_end_col].to_numpy()
147+
)
141148

142149
# PolarAxes always expects x = theta (radians) and y = r.
143150
# When theta = "y" we need to swap the columns.
144151
if self.theta == "y":
145152
data["x"], data["y"] = data["y"].copy(), data["x"].copy()
153+
if has_endpoints:
154+
data["xend"], data["yend"] = (
155+
data["yend"].copy(),
156+
data["xend"].copy(),
157+
)
146158

147159
return data
148160

tests/test_coord_polar.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
from plotnine.coords.coord_polar import coord_polar
5+
6+
7+
def test_coord_polar_transforms_segment_endpoints_theta_x():
8+
coord = coord_polar(theta="x")
9+
coord.params = {"theta_range": (0, 10), "r_range": (0, 10)}
10+
data = pd.DataFrame({"x": [0], "y": [1], "xend": [10], "yend": [2]})
11+
12+
out = coord.transform(data, None)
13+
14+
assert out.loc[0, "x"] == 0
15+
assert out.loc[0, "y"] == 1
16+
assert np.isclose(out.loc[0, "xend"], 2 * np.pi)
17+
assert out.loc[0, "yend"] == 2
18+
19+
20+
def test_coord_polar_transforms_segment_endpoints_theta_y():
21+
coord = coord_polar(theta="y")
22+
coord.params = {"theta_range": (0, 10), "r_range": (0, 10)}
23+
data = pd.DataFrame({"x": [1], "y": [0], "xend": [2], "yend": [10]})
24+
25+
out = coord.transform(data, None)
26+
27+
assert out.loc[0, "x"] == 0
28+
assert out.loc[0, "y"] == 1
29+
assert np.isclose(out.loc[0, "xend"], 2 * np.pi)
30+
assert out.loc[0, "yend"] == 2

0 commit comments

Comments
 (0)