Dror
Dror

Reputation: 13051

Plot histogram of a large number of integers using matplotlib

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

Upvotes: 0

Views: 9220

Answers (1)

Falko
Falko

Reputation: 17877

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.

Upvotes: 3

Related Questions