From bc9e808a78bbabb4a28db476a04a9696a467abe3 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 11 Sep 2014 19:49:03 -0500 Subject: [PATCH] VIS: Hide labels for NaN/zeros in boxplt [ci skip] --- pandas/tests/test_graphics.py | 23 +++++++++++++++++++++++ pandas/tools/plotting.py | 12 +++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index fffb4f279878f..bcdac274d7282 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -628,6 +628,13 @@ def test_pie_series(self): ax = _check_plot_works(series.plot, kind='pie') self._check_text_labels(ax.texts, series.index) + def test_pie_nan(self): + s = Series([1, np.nan, 1, 1]) + ax = s.plot(kind='pie', legend=True) + expected = ['0', '', '2', '3'] + result = [x.get_text() for x in ax.texts] + self.assertEqual(result, expected) + @slow def test_hist_df_kwargs(self): df = DataFrame(np.random.randn(10, 2)) @@ -2717,6 +2724,22 @@ def test_pie_df(self): self._check_text_labels(ax.texts, labels) self._check_colors(ax.patches, facecolors=color_args) + def test_pie_df_nan(self): + df = DataFrame(np.random.rand(4, 4)) + for i in range(4): + df.iloc[i, i] = np.nan + fig, axes = self.plt.subplots(ncols=4) + df.plot(kind='pie', subplots=True, ax=axes, legend=True) + + base_expected = ['0', '1', '2', '3'] + for i, ax in enumerate(axes): + expected = list(base_expected) # copy + expected[i] = '' + result = [x.get_text() for x in ax.texts] + self.assertEqual(result, expected) + # legend labels + self.assertEqual([x.get_text() for x in ax.get_legend().get_texts()], + base_expected) def test_errorbar_plot(self): d = {'x': np.arange(12), 'y': np.arange(12, 0, -1)} df = DataFrame(d) diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index 0c150074a9298..5e11b459d20d8 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -2033,10 +2033,20 @@ def _make_plot(self): kwds = self.kwds.copy() + def blank_labeler(label, value): + if value == 0: + return '' + else: + return label + idx = [com.pprint_thing(v) for v in self.data.index] labels = kwds.pop('labels', idx) # labels is used for each wedge's labels - results = ax.pie(y, labels=labels, **kwds) + # Blank out labels for values of 0 so they don't overlap + # with nonzero wedges + blabels = [blank_labeler(label, value) for + label, value in zip(labels, y)] + results = ax.pie(y, labels=blabels, **kwds) if kwds.get('autopct', None) is not None: patches, texts, autotexts = results