0

I am trying to plot the frequency of integers in a large list of number in a large range. More specifically:

ints = np.random.random_integers(0,1440,15000)

ints is a long list of integers with values between 0 and 1440. Next I want to plot a histogram that will visualize the frequencies. To that end, I use something like:

fig_per_hour = plt.figure()
per_hour = fig_per_hour.add_subplot(111)
counts, bins, patches = per_hour.hist(
    ints, bins = np.arange(0,1441), normed = False, color = 'g',linewidth=0)
plt.show()

But I face two problems:

  1. An annoying gap at the right end of the x-axis. Although I specify the right(?) amount of bins, the plot doesn't reflect it.
  2. Due to the large range from which the ints are sampled, each bar is very thin, and hard to distinct. Is it possible to stretch the x-axis so the bars will be wider? The reason is that I want to annotate the x-axis and some of the bars.

For reference, here's the output I have so far: enter image description here

1
  • you can specify plt.xlimits(min,max) to make sure the x-axis limits are within your range. and plt.hist(data, bins = range(min,max+binwidth,binwidth)) to make sure the bins are equally distributed Commented Oct 2, 2014 at 8:31

1 Answer 1

3

I'd use set_xlim and a smaller number of bins, e.g. bins = 100:

from matplotlib import pyplot as plt
import numpy as np

ints = np.random.random_integers(0,1440,15000)

fig_per_hour = plt.figure()
per_hour = fig_per_hour.add_subplot(111)
counts, bins, patches = per_hour.hist(
    ints, bins = 100, normed = False, color = 'g',linewidth=0)
plt.gca().set_xlim(ints.min(), ints.max())
plt.show()

enter image description here


Edit: You can manually resized window:

enter image description here

In principal you can do this programmatically with plt.figure(figsize=(20, 2)). But somehow the window size is limited to the screen size.

Sign up to request clarification or add additional context in comments.

3 Comments

This helps a lot, but I'd still like to have enough (=1440) bins. I am interested in the "per minute" behavior over 24hrs period. I don't mind that the width of the resulting image will be huge.
@Dror: I don't really get the application. Can't you resize the figure window manually? You can also zoom and pan the axes. Or do you need to do it programmatically?
A programmatic solution would be best for me, as I want to generate images that will be automatically integrated in a document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.