0

I'm using matplotlib to create a histogram with bins fixed to be one pixel wide. I would like to get rid of the white space between these bins. This essentially means setting the x axis to be one pixel per value on the axis (i.e.: there should 100 bins per 100 pixels on the x axis).

This is the code I am currently using:

def gen_histogram(data, binsize, title=None, minimum=None, maximum=None):

    import matplotlib.pyplot as plt
    plt.hold(False)

    if minimum is None:
        minimum = 0
    if maximum is None:
        maximum = 18207

    (n, bins, patches) = plt.hist(data, bins=numpy.arange(minimum, maximum + binsize, binsize), width=1)
    plt.xlabel('z')
    plt.ylabel('% of total')
    plt.title(title, loc='left', fontsize=11)

So, I want all the bins to be touching each other while remaining one pixel in width. Here is a picture of my current output, https://www.dropbox.com/s/j09sgk56m2m3jio/partIdx.png?dl=0.

Ed Smith
  • 12,716
  • 2
  • 43
  • 55
ztweed
  • 9
  • 4
  • 1
    I am not sure what you are trying to do here, could you add some test data and a picture of what you are getting and describe what you want different? However, I do want to make some comments already: 1) don't import things inside a function, 2) setting parameters to None in the function call and then overwriting them later is a bit odd, rather change the function call to minimum=0, maximum=18207 and 3) why are you using plt.hold(False)? – Bas Jansen Sep 22 '15 at 16:08
  • A picture of my current output is below. All I want to do is get rid of the spaces that you can see between the individual bins in the plot. https://www.dropbox.com/s/j09sgk56m2m3jio/partIdx.png?dl=0 – ztweed Sep 22 '15 at 18:05
  • 2
    are you sure you have data in all of your bins? Can you update your question with some synthetic data so we can run your example? – tacaswell Sep 22 '15 at 18:09
  • yes, I do have data in consecutive bins. I amended the first paragraph, so my problem should hopefully be clearer. – ztweed Sep 22 '15 at 18:27
  • Does `plt.axis('tight')` help? – Ed Smith Sep 23 '15 at 08:15

0 Answers0