#!/usr/bin/env python
# coding: utf-8
# #
#
# A few examples of using Lets-Plot with dictionaries, Pandas DataFrames and Polars DataFrames.
# ### Table of Contents
#
# 1. [Python Dictionaries](#dict)
#
# 2. [Pandas Dataframe](#pandas)
#
# 2.1. [From Dictionary](#pandas-from-dict)
#
# 2.2. [From CSV](#pandas-from-csv)
#
# 3. [Polars Dataframe](#polars)
#
# 3.1. [From Dictionary](#polars-from-dict)
#
# 3.2. [From CSV](#polars-from-csv)
# In[1]:
import numpy as np
import pandas as pd
import polars as pl
from lets_plot import *
# In[2]:
LetsPlot.setup_html()
#
#
# ### 1. Python Dictionaries
# In[3]:
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.sin(x)
# In[4]:
ggplot({'x': x, 'y': y}, aes('x', 'y')) + geom_point()
#
#
# ### 2. Pandas Dataframe
#
#
# #### 2.1. From Dictionary
# In[5]:
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}
# In[6]:
pandas_df = pd.DataFrame(get_data_dict())
print(pandas_df.shape)
pandas_df.head()
# In[7]:
ggplot(pandas_df) + \
geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')
#
#
# #### 2.2. From CSV
# In[8]:
# Load mpg dataset with pandas
mpg_pandas_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
# In[9]:
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
# In[10]:
polars_df = pl.DataFrame(get_data_dict())
print(polars_df.shape)
polars_df.head()
# In[11]:
ggplot(polars_df) + \
geom_point(aes('x', 'y', fill='y'), shape=21, size=5, color='white')
#
#
# #### 3.2. From CSV
# In[12]:
# Load mpg dataset with polars
mpg_polars_df = pl.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
# In[13]:
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)