Download notebook (.ipynb)

#

A few examples of using Lets-Plot with dictionaries, Pandas DataFrames and Polars DataFrames.

import numpy as np
import pandas as pd
import polars as pl

from lets_plot import *
LetsPlot.setup_html()

1. Python Dictionaries#

x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.sin(x)
ggplot({'x': x, 'y': y}, aes('x', 'y')) + geom_point()

2. Pandas Dataframe#

2.1. From Dictionary#

def get_data_dict():
    np.random.seed(42)
    n = 100
    x = np.random.uniform(-1, 1, size=n)
    y = 25 * x ** 2 + np.random.normal(size=n)
    return {'x': x, 'y': y}
pandas_df = pd.DataFrame(get_data_dict())
print(pandas_df.shape)
pandas_df.head()
(100, 2)
x y
0 -0.250920 1.661065
1 0.901429 20.015331
2 0.463988 5.473880
3 0.197317 -1.014219
4 -0.687963 11.612646
ggplot(pandas_df) + \
    geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')

2.2. From CSV#

# Load mpg dataset with pandas

mpg_pandas_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
ggplot(mpg_pandas_df, aes('displ', 'cty', fill='drv', size='hwy')) + \
    geom_point(shape=21) + \
    scale_size(range=[5, 15], breaks=[15, 40]) + \
    ggsize(600, 350)

3. Polars Dataframe#

3.1. From Dictionary#

polars_df = pl.DataFrame(get_data_dict())
print(polars_df.shape)
polars_df.head()
(100, 2)
shape: (5, 2)
xy
f64f64
-0.250921.661065
0.90142920.015331
0.4639885.47388
0.197317-1.014219
-0.68796311.612646
ggplot(polars_df) + \
    geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')

3.2. From CSV#

# Load mpg dataset with polars

mpg_polars_df = pl.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
ggplot(mpg_polars_df, aes('displ', 'cty', fill='drv', size='hwy')) + \
    geom_point(shape=21) + \
    scale_size(range=[5, 15], breaks=[15, 40]) + \
    ggsize(600, 350)