Skip to content

Hist showing wrong first bin #4212

Closed
Closed

Description

@tritemio

There is a visualization glitch with matplotlib hist function.

Running this code on an ipython notebook:

from __future__ import print_function
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
%matplotlib inline

tau = 10
N = 1000
exp_fraction = 0.1
N_exp = int(N*exp_fraction)

sample = stats.expon.rvs(0, tau, size=N_exp)
print(sample.shape, sample.mean(), sample.std())
plt.hist(sample, bins=np.arange(-10, 50, 4))
sample[sample < 0]

results in the following histogram:

figure

You can see the first bin (close to -10) is not exactly 0 even if sample has no negative values.

Activity

  1. cimarronm commented on Mar 12, 2015

    @cimarronm
    Contributor

    The problem occurs only on the first bin as that patch has _snap set to False while the other patches do not. This occurs from within axes.hist code:

           for (patch, lbl) in zip_longest(patches, labels, fillvalue=None):
                if patch:
                    p = patch[0]
                    p.update(kwargs)
                    if lbl is not None:
                        p.set_label(lbl)
    
                    p.set_snap(False)
    
                    for p in patch[1:]:
                        p.update(kwargs)
                        p.set_label('_nolegend_')

    In this code, patch is a collection of patches so patch[0] has calls set_snap(False) while the patch[1:] are left with the default snapping which is set.

    The set_snap(False) appears to be a fix for issue #408. @mdboom, perhaps you can take a look. It seemed like you had an idea on the real fix from one of your comments.

  2. cimarronm commented on Mar 12, 2015

    @cimarronm
    Contributor

    @tritemio, as a workaround for your issue you can replace the plt.hist command with the following to get what you were expecting

    ...
    _, _, collection = plt.hist(sample, bins=np.arange(-10, 50, 4))
    collection[0].set_snap(True)
    ...
  3. tritemio commented on Mar 12, 2015

    @tritemio
    Author

    @cimarronm thanks for the workaround! I hope this bug will be fixed soon (definitely before the next release) as it can be pretty embarrassing.

  4. added this to the milestone on Mar 12, 2015
  5. modified the milestones: , on Oct 26, 2015
  6. modified the milestones: , on Mar 21, 2016
  7. pierre-haessig commented on Aug 31, 2016

    @pierre-haessig
    SponsorContributor

    I was about to post on mailing, but now I see it was already reported.

    One detail: I can only reproduce it in a Notebook (inline backend), not in a regular script. Is it clear why ? (In both cases, I have collection[0].get_snap() returning False )

  8. jenshnielsen commented on Aug 31, 2016

    @jenshnielsen
    Member

    It's depending on snapping to pixel borders so it's going to depend on the exact size of the plot you are creating how vissible this is. The inline backend trims to plot to remove whitespace

  9. pierre-haessig commented on Aug 31, 2016

    @pierre-haessig
    SponsorContributor

    right. I didn't find the relevant rcParams which are changed by the %matplotlib inline magic. I noticed 'figsize' was reduced from (8,6) to (6,4), but there is something else: the png from notebook has much less pixels than the png from a script.

  10. jenshnielsen commented on Aug 31, 2016

    @jenshnielsen
    Member

    The inline backend is defined in IPython. It passes bbox_inches='tight' to savefig which crops off any whitespace on the figure making it smaller than the requested size.

  11. efiring commented on Sep 6, 2016

    @efiring
    Member

    Closed by #6887.

  12. modified the milestones: , on Sep 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions