Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e60d080

Browse files
committed
Rotate caps on polar errorbar plots
1 parent ac2a145 commit e60d080

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Polar errorbar caps are rotated
2+
-------------------------------
3+
When plotting errorbars on a polar plot, the caps are now rotated so they are
4+
perpendicular to the errorbars.

lib/matplotlib/axes/_axes.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3439,10 +3439,11 @@ def _upcast_err(err):
34393439
eb_cap_style['color'] = ecolor
34403440

34413441
barcols = []
3442-
caplines = []
3442+
caplines = {'x': [], 'y': []}
34433443

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

34473448
# dep: dependent dataset, indep: independent dataset
34483449
for (dep_axis, dep, err, lolims, uplims, indep, lines_func,
@@ -3486,7 +3487,7 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
34863487
line = mlines.Line2D(indep_masked, indep_masked,
34873488
marker=marker, **eb_cap_style)
34883489
line.set(**{f"{dep_axis}data": lh_masked})
3489-
caplines.append(line)
3490+
caplines[dep_axis].append(line)
34903491
for idx, (lims, hl) in enumerate([(lolims, high), (uplims, low)]):
34913492
if not lims.any():
34923493
continue
@@ -3500,15 +3501,28 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
35003501
line = mlines.Line2D(x_masked, y_masked,
35013502
marker=hlmarker, **eb_cap_style)
35023503
line.set(**{f"{dep_axis}data": hl_masked})
3503-
caplines.append(line)
3504+
caplines[dep_axis].append(line)
35043505
if capsize > 0:
3505-
caplines.append(mlines.Line2D(
3506+
caplines[dep_axis].append(mlines.Line2D(
35063507
x_masked, y_masked, marker=marker, **eb_cap_style))
35073508

3508-
for l in caplines:
3509-
self.add_line(l)
3509+
for axis in caplines:
3510+
for l in caplines[axis]:
3511+
if self.name == 'polar':
3512+
# Rotate caps to be perpendicular to the error bars
3513+
for theta, r in zip(l.get_xdata(), l.get_ydata()):
3514+
rotation = theta
3515+
if axis == 'x':
3516+
rotation += np.pi / 2
3517+
ms = mmarkers.MarkerStyle(marker=marker)
3518+
ms._transform = mtransforms.Affine2D().rotate(rotation)
3519+
self.add_line(mlines.Line2D([theta], [r], marker=ms,
3520+
**eb_cap_style))
3521+
else:
3522+
self.add_line(l)
35103523

35113524
self._request_autoscale_view()
3525+
caplines = caplines['x'] + caplines['y']
35123526
errorbar_container = ErrorbarContainer(
35133527
(data_line, tuple(caplines), tuple(barcols)),
35143528
has_xerr=(xerr is not None), has_yerr=(yerr is not None),

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,16 @@ def test_errorbar():
33763376
ax.set_title("Simplest errorbars, 0.2 in x, 0.4 in y")
33773377

33783378

3379+
@image_comparison(['errorbar_polar_caps'], extensions=['png'])
3380+
def test_errorbar_polar_caps():
3381+
fig = plt.figure()
3382+
ax = plt.subplot(111, projection='polar')
3383+
theta = np.arange(0, 2*np.pi, np.pi / 4)
3384+
r = theta / np.pi / 2 + 0.5
3385+
ax.errorbar(theta, r, xerr=0.15, yerr=0.1)
3386+
ax.set_rlim(0, 2)
3387+
3388+
33793389
def test_errorbar_colorcycle():
33803390

33813391
f, ax = plt.subplots()

0 commit comments

Comments
 (0)