Sie sind auf Seite 1von 2

Real Python Cheat Sheet (realpython.

com)

Building Histograms in Python: Examples

Python offers a handful of different options for building and plotting histograms, all covered in our in-depth tutorial at realpython.com/python-
histograms/. Note: This cheat sheet uses **kwargs in place of some named keyword arguments. Click on each function or method to link to the full
documentation.

collections.Counter(*args, **kwds) matplotlib.pyplot.hist(x, bins=None, range=None, den-


sity=None, **kwargs)

Dict subclass for counting items, housed in Python’s Standard Library.


Compute and plot a histogram. Returns a tuple (n, bins, patches).

>>> a = (0, 1, 1, 1, 2, 3, 7, 7, 23) >>> n, bins, patches = plt.hist(x=d, bins='auto', color='#0504aa',


>>> counts = Counter(a) ... alpha=0.7, rwidth=0.85)

numpy.histogram(a, bins=10, range=None, normed=False, pandas.Series.hist.plot(bins=10, ax=None, grid=True,


weights=None, density=None) **kwargs)

Draw histogram of the input series. See also: pd.DataFrame.hist.plot().


Compute the histogram of a set of data. Returns (hist, bin_edges)

>>> n = np.asarray(np.random.randint(0, 10, size=1000) * 10,


>>> d = np.random.laplace(loc=15, scale=3, size=500) ... dtype=np.int8)
>>> hist, bin_edges = np.histogram(d) >>> ax = s.plot.hist(bins=12, alpha=0.5, figsize=(8, 4))
pandas.Series.value_counts(normalize=False, sort=True, as- seaborn.distplot(a, bins=None, hist=True, kde=True, fit=None,
cending=False, bins=None, dropna=True) **kwargs)

Frequency counts of each distinct value. Excludes NaN values by de-


Combines histogram with KDE or fitted scipy.stats distribution.
fault.

>>> s = pd.Series([1, 1, 1, 4, 4, 5]) >>> x = np.random.randn(100)


>>> s.value_counts() >>> ax = sns.distplot(x, bins='FD')

Das könnte Ihnen auch gefallen