Pandas Hexbin, Hist, Pie, Scatter Plot

The Pandas DataFrame/Series has several methods related to plotting.


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 Hexbin

The dataframe.plot.hexbin() method establishes a relationship between two (2) numeric values. This occurs when there is a large number of data points. With no overlaps, the chart splits into different hexbins.

πŸ’‘ Note: The darker the color hue, the more concentrated the points.

The syntax for this method is as follows:

DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs)
ParameterDescription
xThis parameter is a column label/position for x-points.
yThis parameter is a column label/position for y-points.
cA column integer/string representing the value of an (x, y) point.
reduce_c_functionThis function reduces multiple values in a bin to a single value.
gridsizeThe number of hexagons in the x-direction. Grid size can also be a tuple with two (2) elements indicating x-y numbers.
**kwargsKeywords documented in DataFrame.plot().

For this example,  we have a CSV file containing the Sacramento, California, real-estate sales transactions over a five (5) day span. In addition, a Hexbin chart displays the square footage and house prices.

df = pd.read_csv('real-estate.csv', usecols=['sq__ft', 'price'])
ax = plot.gca()
ax = df.plot.hexbin(x='sq__ft', y='price', gridsize=20, ax=ax)
plot.show()
  • Line  [1] reads in two (2) columns from a comma-delimited CSV file and saves it to df.
  • Line [2] gets the current axes (gca()) and saves it to ax.
  • Line [3] does the following:
    • plots the Hexbin chart based on square footage and house prices
    • sets the grid size to 20
    • sets the ax variable created above
  • Line [4] displays the Hexbin 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 'hexbin' option.

This example uses the NumPy library to plot random numbers using Hexbin.

n  = 900
x  = np.random.uniform(-3, 3, size=n)
y  = np.random.uniform(20, 80, size=n)
ob = np.random.randint(1, 5, size=n)

df = pd.DataFrame({'x': x, 'y': y, 'ob': ob)})
ax = df.plot.hexbin(x='x', y='y', reduce_C_function=np.sum, gridsize=10, cmap="plasma") 
plot.show()
  • Line  [1] sets the size (range) to 900 and saves to n.
  • Line [2-3] uses np.random.uniform to evenly distribute numbers between a specified range.
  • Line [4] uses np.random.randint returns random integers between the specified range.
  • Line [5] creates a DataFrame based on the variables created above and saves it to df.
  • Line [6] does the following:
    • plots the Hexbin chart based on the variables x, and y
    • reduces the plot size by adding up the numbers
    • sets the grid size to 10
    • sets the colormap (cmap) to plasma
  • Line [7] displays the Hexbin 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 'hexbin' option.


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.


DataFrame Plot Pie

The dataframe.plot.pie() method generates a Pie Chart based on a proportional representation of the numeric values in a column.

The syntax for this method is as follows:

DataFrame.plot.pie(**kwargs)
ParameterDescription
yThis parameter is the label/position of the column to plot.
**kwargsKeywords documented in DataFrame.plot().

For this example, Rivers Clothing plots its Quarterly Sales on a Pie Chart.

rivers_dict = {'Months': ['Jan','Aor','Jul','Oct'],
               'Sales':  [28744, 32600, 45700, 55900]}
df = pd.DataFrame(rivers_dict)

qtitle   = 'Rivers Clothing Quarterly Sales'
qlabels  = ['Q1','Q2','Q3','Q4']
qcolors  = ['#9932CC', '#8B008B', '#E6E6FA', '#9370DB']
qexplode = (0,0,0,0.2)

df.plot.pie(title=qtitle, y='Sales', figsize=(6,5), fontsize=9, 
	    labels=qlabels, colors=qcolors, 
            explode=qexplode, legend=False)
plt.show()
  • Line  [1] creates a dictionary of lists with quarterly sale details. This output saves to rivers_dict.
  • Line [2] creates a DataFrame from the dictionary created above.
  • Line [3] saves the title for the Pie chart to qtitle.
  • Line [4] saves the labels for the Pie chart to qlabels.
  • Line [5] saves the slices of the Pie chart to qcolors.
  • Line [6] saves the explode value (away from the main chart) to qexplode.
  • Line [7] creates a Pie chart using the parameters saved above.
  • Line [8] displays the Pie 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 'pie' option.


Further Learning Resources

This is Part 20 of the DataFrame method series.

  • Part 1 focuses on the DataFrame methods abs(), all(), any(), clip(), corr(), and corrwith().
  • Part 2 focuses on the DataFrame methods count(), cov(), cummax(), cummin(), cumprod(), cumsum().
  • Part 3 focuses on the DataFrame methods describe(), diff(), eval(), kurtosis().
  • Part 4 focuses on the DataFrame methods mad(), min(), max(), mean(), median(), and mode().
  • Part 5 focuses on the DataFrame methods pct_change(), quantile(), rank(), round(), prod(), and product().
  • Part 6 focuses on the DataFrame methods add_prefix(), add_suffix(), and align().
  • Part 7 focuses on the DataFrame methods at_time(), between_time(), drop(), drop_duplicates() and duplicated().
  • Part 8 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 9 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 10 focuses on the DataFrame methods reset_index(), sample(), set_axis(), set_index(), take(), and truncate()
  • Part 11 focuses on the DataFrame methods backfill(), bfill(), fillna(), dropna(), and interpolate()
  • Part 12 focuses on the DataFrame methods isna(), isnull(), notna(), notnull(), pad() and replace()
  • Part 13 focuses on the DataFrame methods drop_level(), pivot(), pivot_table(), reorder_levels(), sort_values() and sort_index()
  • Part 14 focuses on the DataFrame methods nlargest(), nsmallest(), swap_level(), stack(), unstack() and swap_axes()
  • Part 15 focuses on the DataFrame methods melt(), explode(), squeeze(), to_xarray(), t() and transpose()
  • Part 16 focuses on the DataFrame methods append(), assign(), compare(), join(), merge() and update()
  • Part 17 focuses on the DataFrame methods asfreq(), asof(), shift(), slice_shift(), tshift(), first_valid_index(), and last_valid_index()
  • Part 18 focuses on the DataFrame methods resample(), to_period(), to_timestamp(), tz_localize(), and tz_convert()
  • Part 19 focuses on the visualization aspect of DataFrames and Series via plotting, such as plot(), and plot.area().
  • Part 20 focuses on continuing the visualization aspect of DataFrames and Series via plotting such as hexbin, hist, pie, and scatter plots.
  • Part 21 focuses on the serialization and conversion methods from_dict(), to_dict(), from_records(), to_records(), to_json(), and to_pickles().
  • Part 22 focuses on the serialization and conversion methods to_clipboard(), to_html(), to_sql(), to_csv(), and to_excel().
  • Part 23 focuses on the serialization and conversion methods to_markdown(), to_stata(), to_hdf(), to_latex(), to_xml().
  • Part 24 focuses on the serialization and conversion methods to_parquet(), to_feather(), to_string(), Styler.
  • Part 25 focuses on the serialization and conversion methods to_bgq() and to_coo().

Also, have a look at the Pandas DataFrame methods cheat sheet!