pythonbar-chartcumulative-sum

Why does matplotlib.pyplot.bar show additional x-values sometimes?


I have written some code that produces a cumulative sum of an array of values. I am trying to plot a bar chart of the values for a statistical comparison test between the orange and blue datasets in the figure below. However, even though I set my x values to range from 0 to 1, it plots the data starting from around -1.25. Absolutely nowhere in my code are my values ever negative, in fact, they physically cannot be negative so I'm not sure why it's doing this.

    cs = np.cumsum(size)
    ncs = cs / np.max(cs)
    plt.bar(np.linspace(0,1,len(ncs)),ncs,label=r)
    plt.legend()

"size" is the array of values (length of 265 - blue and 212 - orange); cs is the cumulative sum of the values; ncs is the normalized cumulative sum to the maximum value of cs. When I print out either "ncs" or "np.linspace(0,1,len(ncs))" no negative values are shown. Even plotting cs as well does not produce any negative values.

I don't get any errors when this runs so it seems that matplotlib is functioning correctly. I've used the bar plot method before without error so I've never seen this before. Any ideas are welcome. My thinking is just that the amount of data is too dense but I'm not sure.

Bar Plot


Solution

  • I cannot be sure this will help since you didn't provide a minimum reproducible example, but try setting the width of the bars to be evenly distributed between 0 and 1:

    plt.bar(np.linspace(0, 1, len(ncs)), ncs, width=(1.0 / len(ncs)), label=r)