Skip to content

VIS: Hide labels for NaN/zeros in boxplt [WIP] #8307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down