Skip to content

Commit 1f504fe

Browse files
author
Tom Augspurger
committed
Merge pull request #8307 from TomAugspurger/pie-nan
VIS: Hide labels for NaN/zeros in boxplt [WIP]
2 parents d1faa4a + bc9e808 commit 1f504fe

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pandas/tests/test_graphics.py

+23
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,13 @@ def test_pie_series(self):
637637
ax = _check_plot_works(series.plot, kind='pie')
638638
self._check_text_labels(ax.texts, series.index)
639639

640+
def test_pie_nan(self):
641+
s = Series([1, np.nan, 1, 1])
642+
ax = s.plot(kind='pie', legend=True)
643+
expected = ['0', '', '2', '3']
644+
result = [x.get_text() for x in ax.texts]
645+
self.assertEqual(result, expected)
646+
640647
@slow
641648
def test_hist_df_kwargs(self):
642649
df = DataFrame(np.random.randn(10, 2))
@@ -2782,6 +2789,22 @@ def test_pie_df(self):
27822789
self._check_text_labels(ax.texts, labels)
27832790
self._check_colors(ax.patches, facecolors=color_args)
27842791

2792+
def test_pie_df_nan(self):
2793+
df = DataFrame(np.random.rand(4, 4))
2794+
for i in range(4):
2795+
df.iloc[i, i] = np.nan
2796+
fig, axes = self.plt.subplots(ncols=4)
2797+
df.plot(kind='pie', subplots=True, ax=axes, legend=True)
2798+
2799+
base_expected = ['0', '1', '2', '3']
2800+
for i, ax in enumerate(axes):
2801+
expected = list(base_expected) # copy
2802+
expected[i] = ''
2803+
result = [x.get_text() for x in ax.texts]
2804+
self.assertEqual(result, expected)
2805+
# legend labels
2806+
self.assertEqual([x.get_text() for x in ax.get_legend().get_texts()],
2807+
base_expected)
27852808
def test_errorbar_plot(self):
27862809
d = {'x': np.arange(12), 'y': np.arange(12, 0, -1)}
27872810
df = DataFrame(d)

pandas/tools/plotting.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -2047,10 +2047,20 @@ def _make_plot(self):
20472047

20482048
kwds = self.kwds.copy()
20492049

2050+
def blank_labeler(label, value):
2051+
if value == 0:
2052+
return ''
2053+
else:
2054+
return label
2055+
20502056
idx = [com.pprint_thing(v) for v in self.data.index]
20512057
labels = kwds.pop('labels', idx)
20522058
# labels is used for each wedge's labels
2053-
results = ax.pie(y, labels=labels, **kwds)
2059+
# Blank out labels for values of 0 so they don't overlap
2060+
# with nonzero wedges
2061+
blabels = [blank_labeler(label, value) for
2062+
label, value in zip(labels, y)]
2063+
results = ax.pie(y, labels=blabels, **kwds)
20542064

20552065
if kwds.get('autopct', None) is not None:
20562066
patches, texts, autotexts = results

0 commit comments

Comments
 (0)