Pandas DataFrame Plot - Scatter and Hexbin Chart

visibility 982 access_time 2 years ago languageEnglish timeline Stats
timeline Stats
Page index 1.25
Pandas DataFrame Plot - Scatter and Hexbin Chart

 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 2 years ago copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

expand_more
Plotting with Pandas DataFrame

More from Kontext
[Diagram] Diagram - Connect to HBase in Python image
visibility 40
thumb_up 0
access_time 3 months ago
Diagram - Connect to HBase in Python
Convert List to Spark Data Frame in Python / Spark
visibility 11,086
thumb_up 0
access_time 2 years ago
Implement SCD Type 2 Full Merge via Spark Data Frames
visibility 22,270
thumb_up 8
access_time 2 years ago