Skip to content

Commit 647a7c4

Browse files
committed
BUG: grouped hist raises error with single group
1 parent 3a7caf9 commit 647a7c4

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

doc/source/v0.14.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,5 @@ Bug Fixes
280280
- Bug in ``pandas.core.strings.str_contains`` does not properly match in a case insensitive fashion when ``regex=False`` and ``case=False`` (:issue:`7505`)
281281

282282
- Bug in ``expanding_cov``, ``expanding_corr``, ``rolling_cov``, and ``rolling_corr`` for two arguments with mismatched index (:issue:`7512`)
283+
- Bug in grouped `hist` doesn't handle `rot` kw and `sharex` kw properly (:issue:`7234`)
284+

pandas/tests/test_graphics.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,22 +2272,34 @@ def test_time_series_plot_color_with_empty_kwargs(self):
22722272
def test_grouped_hist(self):
22732273
df = DataFrame(randn(500, 2), columns=['A', 'B'])
22742274
df['C'] = np.random.randint(0, 4, 500)
2275+
df['D'] = ['X'] * 500
2276+
22752277
axes = plotting.grouped_hist(df.A, by=df.C)
22762278
self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
22772279

22782280
tm.close()
22792281
axes = df.hist(by=df.C)
22802282
self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
22812283

2284+
tm.close()
2285+
# group by a key with single value
2286+
axes = df.hist(by='D', rot=30)
2287+
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
2288+
self._check_ticks_props(axes, xrot=30)
2289+
22822290
tm.close()
22832291
# make sure kwargs to hist are handled
2292+
xf, yf = 20, 18
2293+
xrot, yrot = 30, 40
22842294
axes = plotting.grouped_hist(df.A, by=df.C, normed=True,
2285-
cumulative=True, bins=4)
2286-
2295+
cumulative=True, bins=4,
2296+
xlabelsize=xf, xrot=xrot, ylabelsize=yf, yrot=yrot)
22872297
# height of last bin (index 5) must be 1.0
22882298
for ax in axes.ravel():
22892299
height = ax.get_children()[5].get_height()
22902300
self.assertAlmostEqual(height, 1.0)
2301+
self._check_ticks_props(axes, xlabelsize=xf, xrot=xrot,
2302+
ylabelsize=yf, yrot=yrot)
22912303

22922304
tm.close()
22932305
axes = plotting.grouped_hist(df.A, by=df.C, log=True)

pandas/tools/plotting.py

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,23 +2501,12 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
25012501
kwds : other plotting keyword arguments
25022502
To be passed to hist function
25032503
"""
2504-
import matplotlib.pyplot as plt
25052504

25062505
if by is not None:
25072506
axes = grouped_hist(data, column=column, by=by, ax=ax, grid=grid, figsize=figsize,
25082507
sharex=sharex, sharey=sharey, layout=layout, bins=bins,
2508+
xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot,
25092509
**kwds)
2510-
2511-
for ax in axes.ravel():
2512-
if xlabelsize is not None:
2513-
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
2514-
if xrot is not None:
2515-
plt.setp(ax.get_xticklabels(), rotation=xrot)
2516-
if ylabelsize is not None:
2517-
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
2518-
if yrot is not None:
2519-
plt.setp(ax.get_yticklabels(), rotation=yrot)
2520-
25212510
return axes
25222511

25232512
if column is not None:
@@ -2533,21 +2522,12 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
25332522

25342523
for i, col in enumerate(com._try_sort(data.columns)):
25352524
ax = axes[i // ncols, i % ncols]
2536-
ax.xaxis.set_visible(True)
2537-
ax.yaxis.set_visible(True)
25382525
ax.hist(data[col].dropna().values, bins=bins, **kwds)
25392526
ax.set_title(col)
25402527
ax.grid(grid)
25412528

2542-
if xlabelsize is not None:
2543-
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
2544-
if xrot is not None:
2545-
plt.setp(ax.get_xticklabels(), rotation=xrot)
2546-
if ylabelsize is not None:
2547-
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
2548-
if yrot is not None:
2549-
plt.setp(ax.get_yticklabels(), rotation=yrot)
2550-
2529+
_set_ticks_props(axes, xlabelsize=xlabelsize, xrot=xrot,
2530+
ylabelsize=ylabelsize, yrot=yrot)
25512531
fig.subplots_adjust(wspace=0.3, hspace=0.3)
25522532

25532533
return axes
@@ -2607,23 +2587,18 @@ def hist_series(self, by=None, ax=None, grid=True, xlabelsize=None,
26072587
ax.hist(values, bins=bins, **kwds)
26082588
ax.grid(grid)
26092589
axes = np.array([ax])
2590+
2591+
_set_ticks_props(axes, xlabelsize=xlabelsize, xrot=xrot,
2592+
ylabelsize=ylabelsize, yrot=yrot)
2593+
26102594
else:
26112595
if 'figure' in kwds:
26122596
raise ValueError("Cannot pass 'figure' when using the "
26132597
"'by' argument, since a new 'Figure' instance "
26142598
"will be created")
2615-
axes = grouped_hist(self, by=by, ax=ax, grid=grid, figsize=figsize,
2616-
bins=bins, **kwds)
2617-
2618-
for ax in axes.ravel():
2619-
if xlabelsize is not None:
2620-
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
2621-
if xrot is not None:
2622-
plt.setp(ax.get_xticklabels(), rotation=xrot)
2623-
if ylabelsize is not None:
2624-
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
2625-
if yrot is not None:
2626-
plt.setp(ax.get_yticklabels(), rotation=yrot)
2599+
axes = grouped_hist(self, by=by, ax=ax, grid=grid, figsize=figsize, bins=bins,
2600+
xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot,
2601+
**kwds)
26272602

26282603
if axes.ndim == 1 and len(axes) == 1:
26292604
return axes[0]
@@ -2632,6 +2607,7 @@ def hist_series(self, by=None, ax=None, grid=True, xlabelsize=None,
26322607

26332608
def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
26342609
layout=None, sharex=False, sharey=False, rot=90, grid=True,
2610+
xlabelsize=None, xrot=None, ylabelsize=None, yrot=None,
26352611
**kwargs):
26362612
"""
26372613
Grouped histogram
@@ -2658,9 +2634,15 @@ def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
26582634
def plot_group(group, ax):
26592635
ax.hist(group.dropna().values, bins=bins, **kwargs)
26602636

2637+
xrot = xrot or rot
2638+
26612639
fig, axes = _grouped_plot(plot_group, data, column=column,
26622640
by=by, sharex=sharex, sharey=sharey,
26632641
figsize=figsize, layout=layout, rot=rot)
2642+
2643+
_set_ticks_props(axes, xlabelsize=xlabelsize, xrot=xrot,
2644+
ylabelsize=ylabelsize, yrot=yrot)
2645+
26642646
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9,
26652647
hspace=0.5, wspace=0.3)
26662648
return axes
@@ -3094,6 +3076,22 @@ def _get_xlim(lines):
30943076
return left, right
30953077

30963078

3079+
def _set_ticks_props(axes, xlabelsize=None, xrot=None,
3080+
ylabelsize=None, yrot=None):
3081+
import matplotlib.pyplot as plt
3082+
3083+
for ax in _flatten(axes):
3084+
if xlabelsize is not None:
3085+
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
3086+
if xrot is not None:
3087+
plt.setp(ax.get_xticklabels(), rotation=xrot)
3088+
if ylabelsize is not None:
3089+
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
3090+
if yrot is not None:
3091+
plt.setp(ax.get_yticklabels(), rotation=yrot)
3092+
return axes
3093+
3094+
30973095
if __name__ == '__main__':
30983096
# import pandas.rpy.common as com
30993097
# sales = com.load_data('sanfrancisco.home.sales', package='nutshell')

0 commit comments

Comments
 (0)