Pandas DataFrame Plot - Scatter and Hexbin Chart

event 2020-04-04 visibility 1,923 comment 0 insights
more_vert
insights Stats
Pandas DataFrame Plot - Scatter and Hexbin Chart
Raymond Raymond Code Snippets & Tips

Code snippets and tips for various programming languages/frameworks. All code examples are under MIT or Apache 2.0 license unless specified otherwise. 

 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:

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.

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts