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

Setting histogram weights for multiple columns fails #33173

Closed
mave240 opened this issue Mar 31, 2020 · 3 comments · Fixed by #33440
Closed

Setting histogram weights for multiple columns fails #33173

mave240 opened this issue Mar 31, 2020 · 3 comments · Fixed by #33440
Assignees
Labels
Milestone

Comments

@mave240
Copy link

mave240 commented Mar 31, 2020

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
df = pd.DataFrame(dict(zip(['A', 'B'], np.random.randn(2, 100))))
df.plot.hist(weights=0.1*np.ones(shape=(100, 2)))  # fail

Problem description

Trying to plot a histogram of a multi-column data frame with weights fails with ValueError: weights should have the same shape as a. There is no error for a single column. It doesn't work with:

df.plot.hist(weights=0.1*np.ones(shape=(100,)))  # fail

either.

Expected Output

A matplotlib figure.
pandas_hist_weights_bug_01

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.1.post20200323
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : 0.48.0

@TomAugspurger
Copy link
Contributor

Thanks for the report. Can you post the full traceback, and investigate where things are going wrong?

@TomAugspurger TomAugspurger added the Visualization plotting label Mar 31, 2020
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Mar 31, 2020
@mave240
Copy link
Author

mave240 commented Mar 31, 2020

Thanks for the report. Can you post the full traceback, and investigate where things are going wrong?

Traceback (most recent call last):
File "", line 4, in
File "Z:\miniconda3\envs\tf\lib\site-packages\pandas\plotting_core.py", line 1180, in hist
return self(kind="hist", by=by, bins=bins, **kwargs)
File "Z:\miniconda3\envs\tf\lib\site-packages\pandas\plotting_core.py", line 847, in call
return plot_backend.plot(data, kind=kind, **kwargs)
File "Z:\miniconda3\envs\tf\lib\site-packages\pandas\plotting_matplotlib_init_.py", line 61, in plot
plot_obj.generate()
File "Z:\miniconda3\envs\tf\lib\site-packages\pandas\plotting_matplotlib\core.py", line 260, in generate
self._args_adjust()
File "Z:\miniconda3\envs\tf\lib\site-packages\pandas\plotting_matplotlib\hist.py", line 34, in _args_adjust
weights=self.kwds.get("weights", None),
File "<array_function internals>", line 6, in histogram
File "Z:\miniconda3\envs\tf\lib\site-packages\numpy\lib\histograms.py", line 793, in histogram
a, weights = _ravel_and_check_weights(a, weights)
File "Z:\miniconda3\envs\tf\lib\site-packages\numpy\lib\histograms.py", line 301, in _ravel_and_check_weights
'weights should have the same shape as a.')
ValueError: weights should have the same shape as a.

There are two issues I think. The first one emerges when the number of bins is computed in HistPlot._args_adjust(self). There, np.histogram is called with the weights argument unnecessarily passed in. When removed, the plot succeeds if the shape of weights is (100, ), as that matches the individual column (squeezed) shape, which is required by np.histrogram.
However, my guess is that the shape of weights should be the same as that of the data frame itself, (100, 2) in this case, so each column can be weighted individually. That is the second issue which is less trivial to fix.

@charlesdong1991
Copy link
Member

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants