A few examples of using Lets-Plot with dictionaries, Pandas DataFrames and Polars DataFrames.
2.1. From Dictionary
2.2. From CSV
3.1. From Dictionary
3.2. From CSV
import numpy as np
import pandas as pd
import polars as pl
from lets_plot import *
LetsPlot.setup_html()
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.sin(x)
ggplot({'x': x, 'y': y}, aes('x', 'y')) + geom_point()
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')
# 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)
polars_df = pl.DataFrame(get_data_dict())
print(polars_df.shape)
polars_df.head()
(100, 2)
x | y |
---|---|
f64 | f64 |
-0.25092 | 1.661065 |
0.901429 | 20.015331 |
0.463988 | 5.47388 |
0.197317 | -1.014219 |
-0.687963 | 11.612646 |
ggplot(polars_df) + \
geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')
# 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)