Skip to content
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

ENH: groupby.hist bins don't match #22222

Open
javadnoorb opened this issue Aug 6, 2018 · 2 comments
Open

ENH: groupby.hist bins don't match #22222

javadnoorb opened this issue Aug 6, 2018 · 2 comments

Comments

@javadnoorb
Copy link
Contributor

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd
N = 100
df = pd.DataFrame(np.append(np.random.randn(N), np.random.randn(N)/10), columns = ['rand'])
df['group'] = [0]*N + [1]*N
df.groupby('group')['rand'].hist(bins=20, alpha=0.7)

Problem description

When the dynamic range of two groups are not the same, histogram bins each group according to its own range. Ideally these should match, taking the union of dynamic ranges and binning it for all.

The output of above code is:
image

Expected Output

Histograms with bins of equal widths (or maybe an input flag to hist that could do this)

maybe somethings like the output of following code:

bins = np.linspace(-3, 2.5, 20)
df[df['group']==0]['rand'].hist(bins=bins, alpha=0.7)
df[df['group']==1]['rand'].hist(bins=bins, alpha=0.7)

image

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-29-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.3
pytest: 3.5.0
pip: 18.0
setuptools: 39.1.0
Cython: 0.28.1
numpy: 1.14.5
scipy: 1.0.1
pyarrow: None
xarray: None
IPython: 6.3.0
sphinx: 1.7.2
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: 0.2.0
pandas_datareader: None

@WillAyd
Copy link
Member

WillAyd commented Aug 6, 2018

I'd be +1 on this as I do think your proposal makes more sense. Investigation and PRs are always welcome

@WillAyd WillAyd added this to the Contributions Welcome milestone Aug 6, 2018
@javadnoorb
Copy link
Contributor Author

Thanks @WillAyd. I'll try to come up with a PR.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
ns-rse added a commit to AFM-SPM/TopoStats that referenced this issue Apr 12, 2023
Closes #479

Contrary to the title `plotting.py`, which uses Seaborn, produces consistent bin-width when the range of values differs
between images. The Jupyter Notebook (`notebooks/02-Summary-statistics-and-plots.ipynb`) uses Pandas for loading data
and plotting it (a deliberate choice to reduce the number of packages users would encounter) and it is Pandas which
produces the different bin-widths (see [#22222 ENH: groupby.hist bins don't
match](pandas-dev/pandas#22222)).

The Notebook has been updated to show how to use `np.linspace()` across the total range of data.

Docstrings of `tests/test_plotting.py` have also been improved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants