Pandas DataFrame plot.hist() Method

Rate this post

Preparation

Before any data manipulation can occur, four (4) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
  • The Matplotlib library displays a visual graph of a plotted dataset.
  • The Scipy library allows users to manipulate and visualize the data.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

ip install matplotlib

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install scipy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy

DataFrame Plot Hist

The dataframe.plot.hist() (histogram) method plots the number of times different values appear in a dataset.

The syntax for this method is as follows:

DataFrame.plot.hist(by=None, bins=10, **kwargs)
ParameterDescription
byThis parameter is the column in the DataFrame to group by.
noneThis parameter denotes the number of histogram bins to use.
**kwargsKeywords document in DataFrame.plot().

For this example, this code selects a random number between 0 and 36. This number is the total number of slots on a Roulette wheel (0-36 outside the US). A histogram indicates that some numbers appear more than others.

slots = np.random.randint(0, 36, 250)
df    = pd.DataFrame(slots, columns=['slots'])
df['random'] = df['slots'] + slots
ax = df.plot.hist(bins=12, alpha=0.5)
plt.show()
  • Line [1] creates a variable containing 250 random integers between the specified range.
  • Line [2] creates a DataFrame from the slots variable, sets the columns to the same, and saves it to df.
  • Line [3] creates a new DataFrame column based on the existing slots column plus the slots variable.
  • Line [4] does the following:
    • sets the plot type to Hist
    • the bin size to 12 (bars)
    • the alpha (transparency) to 0.5.
  • Line [5] displays the Hist chart on-screen.

Output

The buttons on the bottom left can be used to further manipulate the chart.

💡 Note: Another way to create this chart is with the plot() method and the kind parameter set to the 'hist' option.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.