Skip to content

Commit

Permalink
FIX: Make stem() baseline follow the curvature in polar plots
Browse files Browse the repository at this point in the history
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
  • Loading branch information
timhoffm committed Jan 20, 2025
1 parent 3fb9c09 commit 90e6f7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3186,6 +3186,10 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
marker_y = heads
baseline_x = [np.min(locs), np.max(locs)]
baseline_y = [bottom, bottom]
if self.name == "polar":
# The baseline must be interpolated to follow the curvature
baseline_x = np.linspace(*baseline_x, 200)
baseline_y = np.linspace(*baseline_y, 200)

markerline, = self.plot(marker_x, marker_y,
color=markercolor, linestyle=markerstyle,
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4617,6 +4617,23 @@ def test_stem_orientation():
orientation='horizontal')


def test_stem_polar_baseline():
"""Test that the baseline is sampled so that it will follow the radius."""
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
x = np.linspace(1.57, 3.14, 10)
y = np.linspace(0, 1, 10)
bottom = 0.5
container = ax.stem(x, y, bottom=bottom)
baseline_x, baseline_y = container.baseline.get_data()
# The number of baseline points N_BASELINE_POINTS is an implementation detail.
# Nevertheless, it's simpler and clearer to test with this detail rather than
# to generically test "sufficiently high sampled".
N_BASELINE_POINTS = 200
assert_allclose(baseline_x, np.linspace(x.min(), x.max(), N_BASELINE_POINTS))
assert_allclose(baseline_y, np.full(N_BASELINE_POINTS, bottom)


@image_comparison(['hist_stacked_stepfilled_alpha'])
def test_hist_stacked_stepfilled_alpha():
# make some data
Expand Down

0 comments on commit 90e6f7b

Please sign in to comment.