Skip to content

Rotate errorbar caps in polar plots #21006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions doc/users/next_whats_new/polar_errorbar_caps.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Polar errorbar caps are rotated
-------------------------------
When plotting errorbars on a polar plot, the caps are now rotated so they are
perpendicular to the errorbars.
28 changes: 21 additions & 7 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3439,10 +3439,11 @@ def _upcast_err(err):
eb_cap_style['color'] = ecolor

barcols = []
caplines = []
caplines = {'x': [], 'y': []}

# Vectorized fancy-indexer.
def apply_mask(arrays, mask): return [array[mask] for array in arrays]
def apply_mask(arrays, mask):
return [array[mask] for array in arrays]

# dep: dependent dataset, indep: independent dataset
for (dep_axis, dep, err, lolims, uplims, indep, lines_func,
Expand Down Expand Up @@ -3486,7 +3487,7 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
line = mlines.Line2D(indep_masked, indep_masked,
marker=marker, **eb_cap_style)
line.set(**{f"{dep_axis}data": lh_masked})
caplines.append(line)
caplines[dep_axis].append(line)
for idx, (lims, hl) in enumerate([(lolims, high), (uplims, low)]):
if not lims.any():
continue
Expand All @@ -3500,15 +3501,28 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
line = mlines.Line2D(x_masked, y_masked,
marker=hlmarker, **eb_cap_style)
line.set(**{f"{dep_axis}data": hl_masked})
caplines.append(line)
caplines[dep_axis].append(line)
if capsize > 0:
caplines.append(mlines.Line2D(
caplines[dep_axis].append(mlines.Line2D(
x_masked, y_masked, marker=marker, **eb_cap_style))

for l in caplines:
self.add_line(l)
for axis in caplines:
for l in caplines[axis]:
if self.name == 'polar':
# Rotate caps to be perpendicular to the error bars
for theta, r in zip(l.get_xdata(), l.get_ydata()):
rotation = theta
if axis == 'x':
rotation += np.pi / 2
ms = mmarkers.MarkerStyle(marker=marker)
ms._transform = mtransforms.Affine2D().rotate(rotation)
self.add_line(mlines.Line2D([theta], [r], marker=ms,
**eb_cap_style))
else:
self.add_line(l)

self._request_autoscale_view()
caplines = caplines['x'] + caplines['y']
errorbar_container = ErrorbarContainer(
(data_line, tuple(caplines), tuple(barcols)),
has_xerr=(xerr is not None), has_yerr=(yerr is not None),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,16 @@ def test_errorbar():
ax.set_title("Simplest errorbars, 0.2 in x, 0.4 in y")


@image_comparison(['errorbar_polar_caps'], extensions=['png'])
def test_errorbar_polar_caps():
fig = plt.figure()
ax = plt.subplot(111, projection='polar')
theta = np.arange(0, 2*np.pi, np.pi / 4)
r = theta / np.pi / 2 + 0.5
ax.errorbar(theta, r, xerr=0.15, yerr=0.1)
ax.set_rlim(0, 2)


def test_errorbar_colorcycle():

f, ax = plt.subplots()
Expand Down