Skip to content

Bug in bar plot, leading zeros in data (bar heights) are ignored. #2150

Closed

Description

@danieljfarrell

The bar plot function seems to ignore bar heights that are zero. For example, the data set called "freq1" does not contain a leading zero and the bar heights are correct. The data set "freq2" contains a leading zeros, both of which are ignored in the bar plot.

import pylab 
import numpy as np
faces = np.array(range(11)) # the histogram bin faces
left_face = faces[:-1]     # the left side face of the cell
bin_width = faces[1:]-faces[:-1]
freq1 = [-2,0,3,4,5,6,7,8,9,12]
freq2 = [0,0,3,4,5,6,7,8,9,12] # error with this data set
pylab.bar(left_face, freq1, width=bin_width)
pylab.show()
pylab.bar(left_face, freq2, width=bin_width)
pylab.show()

Activity

  1. dmcdougall commented on Jun 23, 2013

    @dmcdougall
    Member

    I think this is a feature, not a bug. The axes auto-adjust to only the visible elements of the plot. How do you plot a bar of height zero?

    If you want, you may create the illusion the bars are there by adjusting the limits after plotting:

    import pylab 
    import numpy as np
    faces = np.array(range(11)) # the histogram bin faces
    left_face = faces[:-1]     # the left side face of the cell
    bin_width = faces[1:]-faces[:-1]
    freq2 = [0,0,3,4,5,6,7,8,9,12]
    pylab.bar(left_face, freq2, width=bin_width)
    pylab.xlim([faces[0],faces[-1]])  # adjust limits
    pylab.show()
  2. danieljfarrell commented on Jun 24, 2013

    @danieljfarrell
    Author

    I guess I was expecting a consistent behavior between the different plotting function and for the library not to edit the data set I have specified. For example, plot will include leading and trailing zeros.

    Yes, I see what you mean about plotting a bar of zero height. It's just that for some application the zeros actually have meaning!

  3. dmcdougall commented on Jun 24, 2013

    @dmcdougall
    Member

    I guess I was expecting a consistent behavior between the different plotting function and for the library not to edit the data set I have specified. For example, plot will include leading and trailing zeros.

    It's consistent in the sense that the axes only adjusts its own limits to the visible plot elements. When leading zeros are passed to plot, a line is still drawn between them. The line is visible and so the axes adjust to include it. When a leading zero is passed to bar, there is nothing to draw.

    That said, I absolutely understand where your coming from. For bar, perhaps we need to rethink behaviour based on the data passed rather than the data visible.

    Do any others have feelings about this?

  4. danieljfarrell commented on Jun 24, 2013

    @danieljfarrell
    Author

    For bar, perhaps we need to rethink behaviour based on the data passed rather than the data visible.

    I think that would make sense, at least that is the behaviour I was expecting to see. I see where you're coming from though: you want the library to be as helpful as possible by clipping the range. My perspective (as a user) of the same situation was that the library was editing the data set I was trying to visualise.

    I thought it would be interesting to see how other plotting frameworks handle this. I only got around to testing R, they have a function called barplot which does includes the zero height bars,

    x=c(0,0,3,4,5,6,7,8,9,12)
    barplot(x)

    No I don't have any other feelings other than getting the main point across that, from the user, perspective it seems like the library has edited the data set.

  5. tacaswell commented on Jun 24, 2013

    @tacaswell
    Member

    The auto-locators also do not always do what people expect on the high end (see http://stackoverflow.com/questions/17245966/matplotlib-plot-histogram-has-extra-space) by padding the axes to get to the nearest 'nice' value.

  6. added 3 commits that reference this issue on Mar 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions