Pandas DataFrame Plot - Scatter and Hexbin Chart
access_time 2 years ago languageEnglish
more_vert

Pandas DataFrame Plot - Scatter and Hexbin Chart

visibility 459 comment 0

 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:

xy
0-0.326429-2.236740
10.454832-2.747080
20.1327234.515384
3-0.4377083.494672
4-0.264059-3.256577
.........
99950.0686484.059994
99960.9932742.318345
9997-0.895868-4.447368
99980.4227946.256481
99990.4410447.309338

10000 rows × 2 columns

Scatter chart

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

The above code snippet plots the following chart:

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:

The deeper the color, the higher the density.

info Last modified by Administrator 9 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to contribute on Kontext to help others?

Learn more