Skip to content

Commit 26117f0

Browse files
committed
Merge branch 'histbug' of https://github.com/sinhrks/pandas into sinhrks-histbug
Conflicts: doc/source/v0.14.1.txt
2 parents d6eace7 + 647a7c4 commit 26117f0

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
@@ -290,3 +290,5 @@ Bug Fixes
290290
- Bug in ``expanding_cov``, ``expanding_corr``, ``rolling_cov``, and ``rolling_corr`` for two arguments with mismatched index (:issue:`7512`)
291291

292292
- Bug in ``to_sql`` taking the boolean column as text column (:issue:`7678`)
293+
- Bug in grouped `hist` doesn't handle `rot` kw and `sharex` kw properly (:issue:`7234`)
294+

pandas/tests/test_graphics.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,22 +2307,34 @@ def test_time_series_plot_color_with_empty_kwargs(self):
23072307
def test_grouped_hist(self):
23082308
df = DataFrame(randn(500, 2), columns=['A', 'B'])
23092309
df['C'] = np.random.randint(0, 4, 500)
2310+
df['D'] = ['X'] * 500
2311+
23102312
axes = plotting.grouped_hist(df.A, by=df.C)
23112313
self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
23122314

23132315
tm.close()
23142316
axes = df.hist(by=df.C)
23152317
self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
23162318

2319+
tm.close()
2320+
# group by a key with single value
2321+
axes = df.hist(by='D', rot=30)
2322+
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
2323+
self._check_ticks_props(axes, xrot=30)
2324+
23172325
tm.close()
23182326
# make sure kwargs to hist are handled
2327+
xf, yf = 20, 18
2328+
xrot, yrot = 30, 40
23192329
axes = plotting.grouped_hist(df.A, by=df.C, normed=True,
2320-
cumulative=True, bins=4)
2321-
2330+
cumulative=True, bins=4,
2331+
xlabelsize=xf, xrot=xrot, ylabelsize=yf, yrot=yrot)
23222332
# height of last bin (index 5) must be 1.0
23232333
for ax in axes.ravel():
23242334
height = ax.get_children()[5].get_height()
23252335
self.assertAlmostEqual(height, 1.0)
2336+
self._check_ticks_props(axes, xlabelsize=xf, xrot=xrot,
2337+
ylabelsize=yf, yrot=yrot)
23262338

23272339
tm.close()
23282340
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
@@ -2478,23 +2478,12 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
24782478
kwds : other plotting keyword arguments
24792479
To be passed to hist function
24802480
"""
2481-
import matplotlib.pyplot as plt
24822481

24832482
if by is not None:
24842483
axes = grouped_hist(data, column=column, by=by, ax=ax, grid=grid, figsize=figsize,
24852484
sharex=sharex, sharey=sharey, layout=layout, bins=bins,
2485+
xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot,
24862486
**kwds)
2487-
2488-
for ax in axes.ravel():
2489-
if xlabelsize is not None:
2490-
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
2491-
if xrot is not None:
2492-
plt.setp(ax.get_xticklabels(), rotation=xrot)
2493-
if ylabelsize is not None:
2494-
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
2495-
if yrot is not None:
2496-
plt.setp(ax.get_yticklabels(), rotation=yrot)
2497-
24982487
return axes
24992488

25002489
if column is not None:
@@ -2510,21 +2499,12 @@ def hist_frame(data, column=None, by=None, grid=True, xlabelsize=None,
25102499

25112500
for i, col in enumerate(com._try_sort(data.columns)):
25122501
ax = axes[i // ncols, i % ncols]
2513-
ax.xaxis.set_visible(True)
2514-
ax.yaxis.set_visible(True)
25152502
ax.hist(data[col].dropna().values, bins=bins, **kwds)
25162503
ax.set_title(col)
25172504
ax.grid(grid)
25182505

2519-
if xlabelsize is not None:
2520-
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
2521-
if xrot is not None:
2522-
plt.setp(ax.get_xticklabels(), rotation=xrot)
2523-
if ylabelsize is not None:
2524-
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
2525-
if yrot is not None:
2526-
plt.setp(ax.get_yticklabels(), rotation=yrot)
2527-
2506+
_set_ticks_props(axes, xlabelsize=xlabelsize, xrot=xrot,
2507+
ylabelsize=ylabelsize, yrot=yrot)
25282508
fig.subplots_adjust(wspace=0.3, hspace=0.3)
25292509

25302510
return axes
@@ -2584,23 +2564,18 @@ def hist_series(self, by=None, ax=None, grid=True, xlabelsize=None,
25842564
ax.hist(values, bins=bins, **kwds)
25852565
ax.grid(grid)
25862566
axes = np.array([ax])
2567+
2568+
_set_ticks_props(axes, xlabelsize=xlabelsize, xrot=xrot,
2569+
ylabelsize=ylabelsize, yrot=yrot)
2570+
25872571
else:
25882572
if 'figure' in kwds:
25892573
raise ValueError("Cannot pass 'figure' when using the "
25902574
"'by' argument, since a new 'Figure' instance "
25912575
"will be created")
2592-
axes = grouped_hist(self, by=by, ax=ax, grid=grid, figsize=figsize,
2593-
bins=bins, **kwds)
2594-
2595-
for ax in axes.ravel():
2596-
if xlabelsize is not None:
2597-
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
2598-
if xrot is not None:
2599-
plt.setp(ax.get_xticklabels(), rotation=xrot)
2600-
if ylabelsize is not None:
2601-
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
2602-
if yrot is not None:
2603-
plt.setp(ax.get_yticklabels(), rotation=yrot)
2576+
axes = grouped_hist(self, by=by, ax=ax, grid=grid, figsize=figsize, bins=bins,
2577+
xlabelsize=xlabelsize, xrot=xrot, ylabelsize=ylabelsize, yrot=yrot,
2578+
**kwds)
26042579

26052580
if axes.ndim == 1 and len(axes) == 1:
26062581
return axes[0]
@@ -2609,6 +2584,7 @@ def hist_series(self, by=None, ax=None, grid=True, xlabelsize=None,
26092584

26102585
def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
26112586
layout=None, sharex=False, sharey=False, rot=90, grid=True,
2587+
xlabelsize=None, xrot=None, ylabelsize=None, yrot=None,
26122588
**kwargs):
26132589
"""
26142590
Grouped histogram
@@ -2635,9 +2611,15 @@ def grouped_hist(data, column=None, by=None, ax=None, bins=50, figsize=None,
26352611
def plot_group(group, ax):
26362612
ax.hist(group.dropna().values, bins=bins, **kwargs)
26372613

2614+
xrot = xrot or rot
2615+
26382616
fig, axes = _grouped_plot(plot_group, data, column=column,
26392617
by=by, sharex=sharex, sharey=sharey,
26402618
figsize=figsize, layout=layout, rot=rot)
2619+
2620+
_set_ticks_props(axes, xlabelsize=xlabelsize, xrot=xrot,
2621+
ylabelsize=ylabelsize, yrot=yrot)
2622+
26412623
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9,
26422624
hspace=0.5, wspace=0.3)
26432625
return axes
@@ -3044,6 +3026,22 @@ def _get_xlim(lines):
30443026
return left, right
30453027

30463028

3029+
def _set_ticks_props(axes, xlabelsize=None, xrot=None,
3030+
ylabelsize=None, yrot=None):
3031+
import matplotlib.pyplot as plt
3032+
3033+
for ax in _flatten(axes):
3034+
if xlabelsize is not None:
3035+
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
3036+
if xrot is not None:
3037+
plt.setp(ax.get_xticklabels(), rotation=xrot)
3038+
if ylabelsize is not None:
3039+
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
3040+
if yrot is not None:
3041+
plt.setp(ax.get_yticklabels(), rotation=yrot)
3042+
return axes
3043+
3044+
30473045
if __name__ == '__main__':
30483046
# import pandas.rpy.common as com
30493047
# sales = com.load_data('sanfrancisco.home.sales', package='nutshell')

0 commit comments

Comments
 (0)