{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#
\n",
"\n",
"A few examples of using Lets-Plot with dictionaries, Pandas DataFrames and Polars DataFrames."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Table of Contents\n",
"\n",
"1. [Python Dictionaries](#1.-Python-Dictionaries)\n",
"\n",
"2. [Pandas Dataframe](#2.-Pandas-Dataframe)\n",
"\n",
" 2.1. [From Dictionary](#2.1.-From-Dictionary)\n",
"\n",
" 2.2. [From CSV](#2.2.-From-CSV)\n",
"\n",
"3. [Polars Dataframe](#3.-Polars-Dataframe)\n",
"\n",
" 3.1. [From Dictionary](#3.1.-From-Dictionary)\n",
"\n",
" 3.2. [From CSV](#3.2.-From-CSV)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.361126Z",
"iopub.status.busy": "2025-11-05T13:42:48.361036Z",
"iopub.status.idle": "2025-11-05T13:42:48.364180Z",
"shell.execute_reply": "2025-11-05T13:42:48.363996Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import polars as pl\n",
"\n",
"from lets_plot import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.364998Z",
"iopub.status.busy": "2025-11-05T13:42:48.364925Z",
"iopub.status.idle": "2025-11-05T13:42:48.366625Z",
"shell.execute_reply": "2025-11-05T13:42:48.366460Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"LetsPlot.setup_html()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Python Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.380175Z",
"iopub.status.busy": "2025-11-05T13:42:48.380069Z",
"iopub.status.idle": "2025-11-05T13:42:48.381596Z",
"shell.execute_reply": "2025-11-05T13:42:48.381402Z"
}
},
"outputs": [],
"source": [
"x = np.linspace(-2 * np.pi, 2 * np.pi, 100)\n",
"y = np.sin(x)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.382369Z",
"iopub.status.busy": "2025-11-05T13:42:48.382287Z",
"iopub.status.idle": "2025-11-05T13:42:48.410663Z",
"shell.execute_reply": "2025-11-05T13:42:48.410422Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot({'x': x, 'y': y}, aes('x', 'y')) + geom_point()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Pandas Dataframe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.1. From Dictionary"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.411556Z",
"iopub.status.busy": "2025-11-05T13:42:48.411463Z",
"iopub.status.idle": "2025-11-05T13:42:48.413156Z",
"shell.execute_reply": "2025-11-05T13:42:48.412947Z"
}
},
"outputs": [],
"source": [
"def get_data_dict():\n",
" np.random.seed(42)\n",
" n = 100\n",
" x = np.random.uniform(-1, 1, size=n)\n",
" y = 25 * x ** 2 + np.random.normal(size=n)\n",
" return {'x': x, 'y': y}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.413947Z",
"iopub.status.busy": "2025-11-05T13:42:48.413861Z",
"iopub.status.idle": "2025-11-05T13:42:48.417733Z",
"shell.execute_reply": "2025-11-05T13:42:48.417551Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(100, 2)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" x | \n",
" y | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" -0.250920 | \n",
" 1.661065 | \n",
"
\n",
" \n",
" | 1 | \n",
" 0.901429 | \n",
" 20.015331 | \n",
"
\n",
" \n",
" | 2 | \n",
" 0.463988 | \n",
" 5.473880 | \n",
"
\n",
" \n",
" | 3 | \n",
" 0.197317 | \n",
" -1.014219 | \n",
"
\n",
" \n",
" | 4 | \n",
" -0.687963 | \n",
" 11.612646 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" x y\n",
"0 -0.250920 1.661065\n",
"1 0.901429 20.015331\n",
"2 0.463988 5.473880\n",
"3 0.197317 -1.014219\n",
"4 -0.687963 11.612646"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pandas_df = pd.DataFrame(get_data_dict())\n",
"print(pandas_df.shape)\n",
"pandas_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.418485Z",
"iopub.status.busy": "2025-11-05T13:42:48.418414Z",
"iopub.status.idle": "2025-11-05T13:42:48.421812Z",
"shell.execute_reply": "2025-11-05T13:42:48.421633Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(pandas_df) + \\\n",
" geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2.2. From CSV"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.422561Z",
"iopub.status.busy": "2025-11-05T13:42:48.422488Z",
"iopub.status.idle": "2025-11-05T13:42:48.502004Z",
"shell.execute_reply": "2025-11-05T13:42:48.501778Z"
}
},
"outputs": [],
"source": [
"# Load mpg dataset with pandas\n",
"\n",
"mpg_pandas_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.503002Z",
"iopub.status.busy": "2025-11-05T13:42:48.502923Z",
"iopub.status.idle": "2025-11-05T13:42:48.508360Z",
"shell.execute_reply": "2025-11-05T13:42:48.508191Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(mpg_pandas_df, aes('displ', 'cty', fill='drv', size='hwy')) + \\\n",
" geom_point(shape=21) + \\\n",
" scale_size(range=[5, 15], breaks=[15, 40]) + \\\n",
" ggsize(600, 350)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Polars Dataframe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.1. From Dictionary"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.509187Z",
"iopub.status.busy": "2025-11-05T13:42:48.509116Z",
"iopub.status.idle": "2025-11-05T13:42:48.522061Z",
"shell.execute_reply": "2025-11-05T13:42:48.521889Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(100, 2)\n"
]
},
{
"data": {
"text/html": [
"\n",
"
shape: (5, 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 |
"
],
"text/plain": [
"shape: (5, 2)\n",
"┌───────────┬───────────┐\n",
"│ x ┆ y │\n",
"│ --- ┆ --- │\n",
"│ f64 ┆ f64 │\n",
"╞═══════════╪═══════════╡\n",
"│ -0.25092 ┆ 1.661065 │\n",
"│ 0.901429 ┆ 20.015331 │\n",
"│ 0.463988 ┆ 5.47388 │\n",
"│ 0.197317 ┆ -1.014219 │\n",
"│ -0.687963 ┆ 11.612646 │\n",
"└───────────┴───────────┘"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"polars_df = pl.DataFrame(get_data_dict())\n",
"print(polars_df.shape)\n",
"polars_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.522940Z",
"iopub.status.busy": "2025-11-05T13:42:48.522863Z",
"iopub.status.idle": "2025-11-05T13:42:48.526250Z",
"shell.execute_reply": "2025-11-05T13:42:48.526042Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(polars_df) + \\\n",
" geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.2. From CSV"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.527116Z",
"iopub.status.busy": "2025-11-05T13:42:48.527031Z",
"iopub.status.idle": "2025-11-05T13:42:48.605509Z",
"shell.execute_reply": "2025-11-05T13:42:48.605157Z"
}
},
"outputs": [],
"source": [
"# Load mpg dataset with polars\n",
"\n",
"mpg_polars_df = pl.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"execution": {
"iopub.execute_input": "2025-11-05T13:42:48.606756Z",
"iopub.status.busy": "2025-11-05T13:42:48.606590Z",
"iopub.status.idle": "2025-11-05T13:42:48.612495Z",
"shell.execute_reply": "2025-11-05T13:42:48.612311Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(mpg_polars_df, aes('displ', 'cty', fill='drv', size='hwy')) + \\\n",
" geom_point(shape=21) + \\\n",
" scale_size(range=[5, 15], breaks=[15, 40]) + \\\n",
" ggsize(600, 350)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}