ENH: option for groupby.hist to match bins
State: closed

- [x] closes #22222
- [ ] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Example code and result:

```python
%matplotlib inline
import numpy as np
import pandas as pd

def test_hist_bins_match():
    # GH-22222
    N = 100
    bins = 5

    np.random.seed(0)
    df = pd.DataFrame(np.append(np.random.randn(N), np.random.randn(N) / 10),
                   columns=['rand'])
    df['group'] = [0] * N + [1] * N
    g = df.groupby('group')['rand']
    bin_range = np.linspace(df['rand'].min(), df['rand'].max(), bins + 1)

    ax = g.hist(bins=bins, alpha=0.7, equal_bins=True)
    both_hists_max = g.apply(lambda x: max(
        np.histogram(x, bins=bin_range)[0])).max()
    assert ax.iloc[0].get_ylim()[1] >= both_hists_max

test_hist_bins_match()
```

![image](https://user-images.githubusercontent.com/26352146/43753017-add30f56-99d1-11e8-9b20-fecbdfa20fb2.png)


Labels:
name: Groupby
name: Visualization
description: plotting

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=h1) Report
> Merging [#22228](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/ab274c2479ac8d2bd3a24641a479930a7a61b2dd?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `85.71%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/22228/graphs/tree.svg?width=650&height=150&src=pr&token=eZ4WkYLtcO)](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #22228      +/-   ##
==========================================
- Coverage   92.08%   92.08%   -0.01%     
==========================================
  Files         169      169              
  Lines       50691    50698       +7     
==========================================
+ Hits        46681    46687       +6     
- Misses       4010     4011       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.49% <85.71%> (-0.01%)` | :arrow_down: |
| #single | `42.33% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/plotting/\_core.py](https://codecov.io/gh/pandas-dev/pandas/pull/22228/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb3JlLnB5) | `83.48% <ø> (ø)` | :arrow_up: |
| [pandas/core/groupby/groupby.py](https://codecov.io/gh/pandas-dev/pandas/pull/22228/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9ncm91cGJ5LnB5) | `96.04% <85.71%> (-0.12%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=footer). Last update [ab274c2...adccd1a](https://codecov.io/gh/pandas-dev/pandas/pull/22228?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

There is a python2 versus python3 discrepancy that's causing some CI tests to fail. I opened #22285 to deal with it.
One job failed for no reason. Otherwise it's green.
Making a new function that does this is pretty straightforward:
```diff
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index 3f84fa0..2d324f4 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -1001,6 +1001,13 @@ class GroupBy(_GroupBy):
     len(grouped) : int
         Number of groups
     """
+    def new_hist(self, bins=10, equal_bins=False, **kwargs):
+        if equal_bins and is_scalar(bins):
+            # share the same numpy array for all group bins
+            bins = np.linspace(self.obj.min(),
+                               self.obj.max(), bins + 1)
+        return self.hist(bins=bins, **kwargs)
+
     def _bool_agg(self, val_test, skipna):
         """Shared func to call any / all Cython GroupBy implementations"""
```

but I don't know how to overload `SeriesGroupBy.hist` without touching the wrappers, or some other sensitive part of the code. I defer this PR to anyone who knows how to do this.
Closing as stale. Ping if you'd like to pick this back up