Skip to content

Commit 01e1856

Browse files
author
Tom Augspurger
committed
Merge pull request #8183 from sinhrks/scatter_error
BUG: scatter with errorbar raises IndexError
2 parents d2694c8 + c78eb6e commit 01e1856

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

doc/source/v0.15.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ Bug Fixes
650650

651651
- Bug in ``pivot_table`` performed with nameless ``index`` and ``columns`` raises ``KeyError`` (:issue:`8103`)
652652

653+
- Bug in ``DataFrame.plot(kind='scatter')`` draws points and errorbars with different colors when the color is specified by ``c`` keyword (:issue:`8081`)
653654

654655

655656

pandas/tests/test_graphics.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ def _check_has_errorbars(self, axes, xerr=0, yerr=0):
349349
yerr : number
350350
expected number of y errorbar
351351
"""
352-
353352
axes = self._flatten_visible(axes)
354353
for ax in axes:
355354
containers = ax.containers
@@ -2805,12 +2804,28 @@ def test_errorbar_scatter(self):
28052804
self._check_has_errorbars(ax, xerr=0, yerr=0)
28062805
ax = _check_plot_works(df.plot, kind='scatter', x='x', y='y', xerr=df_err)
28072806
self._check_has_errorbars(ax, xerr=1, yerr=0)
2807+
28082808
ax = _check_plot_works(df.plot, kind='scatter', x='x', y='y', yerr=df_err)
28092809
self._check_has_errorbars(ax, xerr=0, yerr=1)
28102810
ax = _check_plot_works(df.plot, kind='scatter', x='x', y='y',
28112811
xerr=df_err, yerr=df_err)
28122812
self._check_has_errorbars(ax, xerr=1, yerr=1)
28132813

2814+
def _check_errorbar_color(containers, expected, has_err='has_xerr'):
2815+
errs = [c.lines[1][0] for c in ax.containers if getattr(c, has_err, False)]
2816+
self._check_colors(errs, linecolors=[expected] * len(errs))
2817+
2818+
# GH 8081
2819+
df = DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
2820+
ax = df.plot(kind='scatter', x='a', y='b', xerr='d', yerr='e', c='red')
2821+
self._check_has_errorbars(ax, xerr=1, yerr=1)
2822+
_check_errorbar_color(ax.containers, 'red', has_err='has_xerr')
2823+
_check_errorbar_color(ax.containers, 'red', has_err='has_yerr')
2824+
2825+
ax = df.plot(kind='scatter', x='a', y='b', yerr='e', color='green')
2826+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2827+
_check_errorbar_color(ax.containers, 'green', has_err='has_yerr')
2828+
28142829

28152830
@tm.mplskip
28162831
class TestDataFrameGroupByPlots(TestPlotBase):

pandas/tools/plotting.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1394,15 +1394,13 @@ def _make_plot(self):
13941394
label = None
13951395
scatter = ax.scatter(data[x].values, data[y].values, label=label,
13961396
**self.kwds)
1397-
13981397
self._add_legend_handle(scatter, label)
13991398

14001399
errors_x = self._get_errorbars(label=x, index=0, yerr=False)
1401-
errors_y = self._get_errorbars(label=y, index=1, xerr=False)
1400+
errors_y = self._get_errorbars(label=y, index=0, xerr=False)
14021401
if len(errors_x) > 0 or len(errors_y) > 0:
14031402
err_kwds = dict(errors_x, **errors_y)
1404-
if 'color' in self.kwds:
1405-
err_kwds['color'] = self.kwds['color']
1403+
err_kwds['ecolor'] = scatter.get_facecolor()[0]
14061404
ax.errorbar(data[x].values, data[y].values, linestyle='none', **err_kwds)
14071405

14081406
def _post_plot_logic(self):

0 commit comments

Comments
 (0)