You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Histogram range appears to be calculated incorrectly when using "bins='auto'" with 2D data #8778
Using 'auto' to compute the number of bins with 2D data only uses the first column to compute the range. This causes all other columns to be incorrectly displayed.
Calculating histograms using bins='auto' with numpy gives the correct behavior. Code for reproduction
frompylabimport*# four values to examinex=randn(1000, 4)*0.1+arange(4)/7figure()
ax1=subplot(211)
plt.hist(x, histtype='step')
legend(['1', '2', '3', '4'])
ax2=subplot(212)
plt.hist(x, bins='auto', histtype='step')
legend(['1', '2', '3', '4'])
# Match axes so it's clear where the problem isax2.set_xlim(ax1.get_xlim())
plt.show()
# Check that numpy doesn't have this bug_, bins_default=np.histogram(x)
_, bins_auto=np.histogram(x, bins='auto')
assert(np.min(bins_default) ==np.min(bins_auto))
assert(np.max(bins_default) ==np.max(bins_auto))
Actual outcome
The range of the histogram corresponds to the range of the last column of data for all columns, see second subplot.
Expected outcome
The range of the histogram should correspond to the min/max of all data, as it is in the first subplot of the example code.
It looks to me that the problem may come from this section in axes.hist:
foriinxrange(nx):
# this will automatically overwrite bins,# so that each histogram uses the same binsm, bins=np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
If the bins parameter that is initially passed to hist is “auto¹”, then bins becomes a sequence after the first iteration, and this iteration will be kept during the following runs. My first idea would be to just add some supplementary logic (in the loop?) to take care of the case of bins being a string.
¹ : maybe I am wrong, but one could pass any string that is valid for numpy.histogram, don't we? Is it deliberate to not mention it in the docstring of ax.hist? Otherwise, it looks like a pretty kind nice but unfortunately hidden feature ^^.
passing any string was intentional since we knew the plan was for numpy to
add more kinds of auto-ranges. If it isn't documented, it should be
something along the lines of "passes this value directly to numpy.hist()".
On Mon, Jun 19, 2017 at 11:15 AM, Adrien F. Vincent < ***@***.***> wrote:
It looks to me that the problem may come from this section
<https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_axes.py#L6102>
in axes.hist:
for i in xrange(nx):
# this will automatically overwrite bins,
# so that each histogram uses the same bins
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
If the bins parameter that is initially passed to hist is “auto¹”, then
bins becomes a sequence after the first iteration, and this iteration will
be kept during the following runs. My first idea would be to just add some
supplementary logic (in the loop?) to take care of the case of bins being
a string.
¹ : maybe I am wrong, but one could pass any string that is valid for
numpy.histogram, don't we? Is it deliberate to not mention it in
ax.docstring? Otherwise, it looks like a pretty kind nice but
unfortunately hidden feature ^^.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8778 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy-GhOb8cYoyVtegtxfo1tfGy9R1xaks5sFpClgaJpZM4N-WCA>
.
Activity
afvincent commentedon Jun 19, 2017
It looks to me that the problem may come from this section in
axes.hist:If the
binsparameter that is initially passed tohistis “auto¹”, then bins becomes a sequence after the first iteration, and this iteration will be kept during the following runs. My first idea would be to just add some supplementary logic (in the loop?) to take care of the case ofbinsbeing a string.¹ : maybe I am wrong, but one could pass any string that is valid for
numpy.histogram, don't we? Is it deliberate to not mention it in the docstring ofax.hist? Otherwise, it looks like a pretty kind nice but unfortunately hidden feature ^^.Edit: English...
WeatherGod commentedon Jun 19, 2017
tacaswell commentedon Jun 19, 2017
Closing as a duplicate of #8636
afvincent commentedon Jun 20, 2017
Indeed (and the on-going PR #8638 may fix this).