Pandas DataFrame Plot - Scatter and Hexbin Chart

Pandas DataFrame Plot - Scatter and Hexbin Chart

Raymond Tang Raymond Tang 0 2078 1.09 index 4/4/2020

In this article I'm going to show you some examples about plotting scatter and hexbin chart with Pandas DataFrame. I'm using Jupyter Notebook as IDE/code execution environment.

Hexbin chart is a pcolor of a 2-D histogram with hexagonal cell and can be more informative compared with Scatter chart.

Prepare the data

Use the following code snippet to create a Pandas DataFrame object in memory:

import pandas as pd
import numpy as np

data = []
n = 10000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
df = pd.DataFrame()
df['x'] = x
df['y'] = y
df

The above code populates a dataframe like the following table:

x y
0 -0.326429
--- ---
1 0.454832
--- ---
2 0.132723
--- ---
3 -0.437708
--- ---
4 -0.264059
--- ---
... ...
--- ---
9995 0.068648
--- ---
9996 0.993274
--- ---
9997 -0.895868
--- ---
9998 0.422794
--- ---
9999 0.441044
--- ---

10000 rows × 2 columns

Scatter chart

plt =df.plot(kind='scatter',x='x', y='y')

The above code snippet plots the following chart:

2020040445951-image.png

Hexbin

plt =df.plot(kind='hexbin',x='x', bins='log',y='y')

For log function is used for creating bins. The chart looks like the following screenshot:

2020040450158-image.png

The deeper the color, the higher the density.

jupyter-notebook pandas pandas-plot plot python

Join the Discussion

View or add your thoughts below

Comments