Skip to content

Commit 90e6f7b

Browse files
committed
FIX: Make stem() baseline follow the curvature in polar plots
Note: There's precedence for a similar special-casing on polar Axes for errorbars: https://github.com/matplotlib/matplotlib/blob/3fb9c0961b5c8d5753c88ac37c08cda58a4b7839/lib/matplotlib/axes/_axes.py#L3798
1 parent 3fb9c09 commit 90e6f7b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,6 +3186,10 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
31863186
marker_y = heads
31873187
baseline_x = [np.min(locs), np.max(locs)]
31883188
baseline_y = [bottom, bottom]
3189+
if self.name == "polar":
3190+
# The baseline must be interpolated to follow the curvature
3191+
baseline_x = np.linspace(*baseline_x, 200)
3192+
baseline_y = np.linspace(*baseline_y, 200)
31893193

31903194
markerline, = self.plot(marker_x, marker_y,
31913195
color=markercolor, linestyle=markerstyle,

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,6 +4617,23 @@ def test_stem_orientation():
46174617
orientation='horizontal')
46184618

46194619

4620+
def test_stem_polar_baseline():
4621+
"""Test that the baseline is sampled so that it will follow the radius."""
4622+
fig = plt.figure()
4623+
ax = fig.add_subplot(projection='polar')
4624+
x = np.linspace(1.57, 3.14, 10)
4625+
y = np.linspace(0, 1, 10)
4626+
bottom = 0.5
4627+
container = ax.stem(x, y, bottom=bottom)
4628+
baseline_x, baseline_y = container.baseline.get_data()
4629+
# The number of baseline points N_BASELINE_POINTS is an implementation detail.
4630+
# Nevertheless, it's simpler and clearer to test with this detail rather than
4631+
# to generically test "sufficiently high sampled".
4632+
N_BASELINE_POINTS = 200
4633+
assert_allclose(baseline_x, np.linspace(x.min(), x.max(), N_BASELINE_POINTS))
4634+
assert_allclose(baseline_y, np.full(N_BASELINE_POINTS, bottom)
4635+
4636+
46204637
@image_comparison(['hist_stacked_stepfilled_alpha'])
46214638
def test_hist_stacked_stepfilled_alpha():
46224639
# make some data

0 commit comments

Comments
 (0)