Jimmys
Jimmys

Reputation: 377

matplotlib histogram in python

I have load a time series dataset in pandas(date/time and kwh) and I would like to build a histogram.I'm confused with the syntax.I used this(plt.hist(data.kwh)) but the result wasn't good.

Upvotes: 0

Views: 121

Answers (2)

lmiguelvargasf
lmiguelvargasf

Reputation: 69755

matplotlib.pyplot.hist(samples, bins = 101)

where samples is an array and bins is the number of bins you want in your histogram.

Alternatively, you can use pylab to performm this task:

pylab.hist(samples, bins = 101)

Upvotes: 1

Tristan Maxson
Tristan Maxson

Reputation: 229

Ideally when you build your histogram you should also use the "bins" parameter to make enough bins to hold your data.

import matplotlib.pyplot as plt
from math import ceil

# Load data as data.kwh

spacing = 10 # size on x axis of the bin to aim for
bins = ceil((data.kwh.max() - data.kwh.min()) / spacing)

plt.hist(data.kwh, bins=bins)
plt.show()

Upvotes: 1

Related Questions