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

Skip to content

Commit ea337ef

Browse files
committed
Rotate caps on polar errorbar plots
1 parent a142369 commit ea337ef

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
@@ -3433,10 +3433,11 @@ def errorbar(self, x, y, yerr=None, xerr=None,
34333433
eb_cap_style['color'] = ecolor
34343434

34353435
barcols = []
3436-
caplines = []
3436+
caplines = {'x': [], 'y': []}
34373437

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

34413442
# dep: dependent dataset, indep: independent dataset
34423443
for (dep_axis, dep, err, lolims, uplims, indep, lines_func,
@@ -3477,7 +3478,7 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
34773478
line = mlines.Line2D(indep_masked, indep_masked,
34783479
marker=marker, **eb_cap_style)
34793480
line.set(**{f"{dep_axis}data": lh_masked})
3480-
caplines.append(line)
3481+
caplines[dep_axis].append(line)
34813482
for idx, (lims, hl) in enumerate([(lolims, high), (uplims, low)]):
34823483
if not lims.any():
34833484
continue
@@ -3491,15 +3492,28 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
34913492
line = mlines.Line2D(x_masked, y_masked,
34923493
marker=hlmarker, **eb_cap_style)
34933494
line.set(**{f"{dep_axis}data": hl_masked})
3494-
caplines.append(line)
3495+
caplines[dep_axis].append(line)
34953496
if capsize > 0:
3496-
caplines.append(mlines.Line2D(
3497+
caplines[dep_axis].append(mlines.Line2D(
34973498
x_masked, y_masked, marker=marker, **eb_cap_style))
34983499

3499-
for l in caplines:
3500-
self.add_line(l)
3500+
for axis in caplines:
3501+
for l in caplines[axis]:
3502+
if self.name == 'polar':
3503+
# Rotate caps to be perpendicular to the error bars
3504+
for theta, r in zip(l.get_xdata(), l.get_ydata()):
3505+
rotation = theta
3506+
if axis == 'x':
3507+
rotation += np.pi / 2
3508+
ms = mmarkers.MarkerStyle(marker=marker)
3509+
ms._transform = mtransforms.Affine2D().rotate(rotation)
3510+
self.add_line(mlines.Line2D([theta], [r], marker=ms,
3511+
**eb_cap_style))
3512+
else:
3513+
self.add_line(l)
35013514

35023515
self._request_autoscale_view()
3516+
caplines = caplines['x'] + caplines['y']
35033517
errorbar_container = ErrorbarContainer(
35043518
(data_line, tuple(caplines), tuple(barcols)),
35053519
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
@@ -3351,6 +3351,16 @@ def test_errorbar():
33513351
ax.set_title("Simplest errorbars, 0.2 in x, 0.4 in y")
33523352

33533353

3354+
@image_comparison(['errorbar_polar_caps'], extensions=['png'])
3355+
def test_errorbar_polar_caps():
3356+
fig = plt.figure()
3357+
ax = plt.subplot(111, projection='polar')
3358+
theta = np.arange(0, 2*np.pi, np.pi / 4)
3359+
r = theta / np.pi / 2 + 0.5
3360+
ax.errorbar(theta, r, xerr=0.15, yerr=0.1)
3361+
ax.set_rlim(0, 2)
3362+
3363+
33543364
def test_errorbar_colorcycle():
33553365

33563366
f, ax = plt.subplots()

0 commit comments

Comments
 (0)