Skip to content

make histtype='step' default #7121

Closed as not planned
Closed as not planned

Description

@tlatorre-uchicago

I'm using matplotlib version 1.5.1 on Scientific Linux 6 (RHEL clone). I am using the matplotlib provided in the anaconda install.

I would like to propose that histograms are drawn by default with histtype='step'. I've been using matplotlib for a long time, and it is very common (at least in high energy physics) to plot histograms with a large number of bins, which using the default drawing behavior leads to very unresponsive plots. For example:

plt.hist(np.random.exponential(size=1000000),bins=10000)
plt.show()

produces a histogram which takes ~15 seconds to draw and takes somewhere between 5-10 seconds to redraw after panning or zooming. In contrast, plotting with histtype='step':

plt.hist(np.random.exponential(size=1000000),bins=10000,histtype='step')
plt.show()

plots almost instantaneously and can be panned and zoomed with no delay.

The step option also follows the least ink principle which Edward Tufte recommends in his book "The Visual Display of Quantitative Information".

At a minimum, maybe the step option could be included in the histogram_demo.py file. I'd be happy to do this if it's a welcome addition.

Activity

  1. efiring commented on Sep 15, 2016

    @efiring
    Member

    I agree that 'step' or 'stepfilled' makes good sense for a histogram. Are there any of our gallery examples or common use cases for which 'step' would work badly? It looks like 'step' is much better as a default than 'stepfilled' because the latter works badly when there is more than one series in the histogram.
    Even if the default is not changed, the hist docstring could note the superior speed of 'step'.

  2. added this to the milestone on Sep 15, 2016
  3. anntzer commented on Sep 16, 2016

    @anntzer
    Contributor

    mwaskom/seaborn#704 / #4065 is an issue about the default appearance of step histograms when using the seaborn styles, although it could be argued this is not a problem of matplotlib itself.

  4. story645 commented on Sep 16, 2016

    @story645
    Member

    I'd vote for it to be included in the demo, but against it being the default since the current default is what's shown in just about every basic stats/research methods book and so I think deviating from that would be massively confusing to a lot of people.

  5. efiring commented on Sep 19, 2016

    @efiring
    Member

    @tlatorre-uchicago, please proceed with a PR to add a 'step' hist example ASAP, and consider including a line somewhere in the hist docstring pointing out the superior speed of step when the number of bins is large. At this point in our development cycle, these changes are helpful and safe; changing this particular default now would evidently be controversial and perhaps undesirable to a major fraction of the user community.

  6. tlatorre-uchicago commented on Sep 19, 2016

    @tlatorre-uchicago
    Author

    Sure, can you recommend which histogram example I should edit? The example that pops up on Google when I search for "matplotlib histogram" is http://matplotlib.org/1.2.1/examples/pylab_examples/histogram_demo.html, but that doesn't appear to be in the documentation anymore.

  7. efiring commented on Sep 19, 2016

    @efiring
    Member

    The docs are in a state of flux, with a trend toward moving things from the pylab section to more specific sections. The latest version actually includes a step-filled example: http://matplotlib.org/devdocs/examples/statistics/histogram_demo_histtypes.html. Source code: https://github.com/matplotlib/matplotlib/blob/master/examples/statistics/histogram_demo_histtypes.py. Maybe we don't need more than that? Or do you want to add a plain step example?

  8. tacaswell commented on Sep 19, 2016

    @tacaswell
    Member

    @tlatorre-uchicago You might also be interested in http://matplotlib.org/devdocs/examples/api/filled_step.html You will have to call np.histogram and step/fill_between, but you will get more control + Line2D objects.

  9. tlatorre-uchicago commented on Sep 19, 2016

    @tlatorre-uchicago
    Author

    Ah, yeah that looks good. I'm not sure what the best course of action is then. I guess there are a couple of options:

    • just add a note to the docstring (should it be in the Notes section at the bottom or in the main paragraph at the top?)
    • add a note to one of the existing examples (histogram_demo_features or histogram_demo_histtypes?)
    • add a new example for "histogramming large datasets" or something
    • add a warning if bins > 1000 and histtype='bar'

    The last option seems a bit hacky from a purist standpoint, but pragmatically that would have alerted me to the problem the quickest.

    I'm happy to do any/all of these. I guess the main objective should be to make sure whatever is changed that it's obvious to anyone in the future that plotting histograms in matplotlib is not slow if you use the histtype='step' argument.

  10. tacaswell commented on Sep 19, 2016

    @tacaswell
    Member

    Adding a note to Notes and extending the histogram_demo_histtypes are probably the best two options.

    👎 on adding a warning, it assumes too much about user intent.

    As a side note, what group are you with at uchicago? I graduated from there a few years ago.

  11. story645 commented on Sep 20, 2016

    @story645
    Member

    I think you can use #7096 as a model.

  12. tlatorre-uchicago commented on Sep 20, 2016

    @tlatorre-uchicago
    Author

    OK, I'll add a note and update the histtypes demo. I'm in Dr. Blucher's group working on SNO+.

  13. tlatorre-uchicago commented on Sep 25, 2016

    @tlatorre-uchicago
    Author

    I'm having some trouble building the HTML documentation.

    1. I have to run python make.py --allowsphinxerrors because there are a lot of sphinx errors. Is this expected?
    2. After building for a while I get the exception 'ascii' codec can't encode character u'\U0001f603' in position 16: ordinal not in range(128)

    The exception comes from line 699 in plot_directive.py: code = textwrap.dedent("\n".join(map(str, content))). I can send the whole traceback if you want.

    I believe I have all of the prerequisites. I've followed the instructions in doc/README.txt. Anything else I should know?

  14. tacaswell commented on Sep 25, 2016

    @tacaswell
    Member

    Try building the docs with python 3.

    There should be no sphinx warnings (which we test for on travis).

  15. 9 remaining items

  16. removed this from the milestone on May 3, 2017
  17. modified the milestones: , on Oct 9, 2017
  18. modified the milestones: , on Feb 10, 2018
  19. modified the milestones: v3.0, v3.1 on Aug 11, 2018
  20. modified the milestones: v3.1.0, v3.2.0 on Mar 18, 2019
  21. anntzer commented on May 23, 2023

    @anntzer
    Contributor

    I suspect changing the default will not happen (other than with some kind of large backcompat-maintaining PR similar how #25427 does it for contour(), which is definitely doable, just a fair bit of work), and we should just try to add references towards stairs(*histogram()) (which is arguably better, though it certainly doesn't replace all use cases) throughout the docs.

  22. timhoffm commented on May 23, 2023

    @timhoffm
    Member

    The documentation is updated. I close this (=change the default) as "not planned".

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions