import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
from ipywidgets import widgets
from IPython.display import display
flavors = ["default", "darcula", "solarized_light", "solarized_dark","high_contrast_light", "high_contrast_dark"]
dropdown_flavors = widgets.Dropdown(options = flavors, description = "Theme flavor:")
from IPython.display import Javascript, display
def run_all(ev):
display(Javascript('IPython.notebook.execute_cells_below()'))
button = widgets.Button(description="Run all with flavor")
button.on_click(run_all)
def withFlavor():
if (dropdown_flavors.value == "darcula"):
return flavor_darcula()
if (dropdown_flavors.value == "solarized_light"):
return flavor_solarized_light()
if (dropdown_flavors.value == "solarized_dark"):
return flavor_solarized_dark()
if (dropdown_flavors.value == "high_contrast_light"):
return flavor_high_contrast_light()
if (dropdown_flavors.value == "high_contrast_dark"):
return flavor_high_contrast_dark()
return theme()
def show(p):
return p + ggsize(450, 300) + withFlavor()
mpg_df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv')
class_df = mpg_df.groupby('class').hwy.agg(['min', 'median', 'max', 'count']).reset_index()
from scipy.stats import multivariate_normal
def generate_random_data(size=50, mean=[0, 0], cov=[[1, .5], [.5, 1]], seed=42):
np.random.seed(seed)
x = np.linspace(-1, 1, size)
y = np.linspace(-1, 1, size)
X, Y = np.meshgrid(x, y)
Z = multivariate_normal(mean, cov).pdf(np.dstack((X, Y)))
return pd.DataFrame({'x': X.flatten(), 'y': Y.flatten(), 'z': Z.flatten()})
random_df = generate_random_data()
midwest_df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/midwest.csv')
pop_df = midwest_df.groupby('state').poptotal.sum().to_frame('population').reset_index()
from lets_plot.geo_data import *
states_df = geocode('state', pop_df.state, scope='US').get_boundaries(9)
The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).
display(dropdown_flavors)
display(button)
Dropdown(description='Theme flavor:', options=('default', 'darcula', 'solarized_light', 'solarized_dark', 'hig…
Button(description='Run all with flavor', style=ButtonStyle())
# POINT
show(
ggplot(mpg_df, aes('cty', 'hwy')) + geom_point()
)
# POINT + shape
show(
ggplot(mpg_df, aes('cty', 'hwy')) + geom_point(shape=22)
)
# JITTER
show( ggplot(mpg_df, aes('fl', 'drv')) + geom_jitter() )
# PATH
t = np.linspace(0, 2 * np.pi, 100)
show(
ggplot({'x': t * np.sin(t), 'y': t * np.cos(t)}, aes(x='x', y='y')) + geom_path()
)
# LINE
x = np.linspace(-4 * np.pi, 4 * np.pi, 100)
y = np.sin(x)
show( ggplot({'x': x, 'y': y}, aes(x='x', y='y')) + geom_line() )
# SEGMENT
show( ggplot() + geom_segment(x=0, y=0, xend=1, yend=1, arrow=arrow()) )
# ABLINE
show( ggplot() + geom_abline(slope=.5))
# HLINE
show( ggplot() + geom_hline(yintercept=0) )
# VLINE
show( ggplot() + geom_vline(xintercept=0) )
# CONTOUR
show( ggplot(random_df, aes('x', 'y')) + geom_contour(aes(z='z')) )
# DENSITY2D
show( ggplot(mpg_df, aes('cty', 'hwy')) + geom_density2d() )
# FREQPOLY
show( ggplot(mpg_df, aes(x='hwy')) + geom_freqpoly() )
# STEP
show( ggplot(mpg_df, aes(x='hwy')) + geom_step(aes(y='..count..'), stat='bin') )
# DENSITY
show( ggplot(mpg_df, aes(x='hwy')) + geom_density() )
# SMOOTH
np.random.seed(42)
n = 50
x = np.arange(n)
y = x + np.random.normal(scale=10, size=n)
show(
ggplot({'x': x, 'y': y}, aes(x='x', y='y')) + geom_smooth()
)
# LOLLIPOP
show(
ggplot({
'x': [-3, -2, -1, 0, 1, 2, 3],
'y': [2, 3, -2, 3, -1, 0, 4],
}, aes('x', 'y')) + geom_lollipop()
)
# DOT_PLOT
show( ggplot(mpg_df, aes(x='hwy')) + geom_dotplot() )
# Y_DOT_PLOT
show(
ggplot(mpg_df, aes('class', 'hwy')) + geom_ydotplot()
)
# BAR
show( ggplot(mpg_df, aes('hwy')) + geom_bar() )
# HISTOGRAM
show( ggplot(mpg_df, aes(x='hwy')) + geom_histogram() )
# PIE
show(
ggplot({'name': ['a', 'b', 'c'], 'value': [40, 90, 10]}) +
geom_pie(aes(slice='value'), stat='identity')
)
# BIN_2D
show( ggplot(mpg_df, aes('cty', 'hwy')) + geom_bin2d() )
# TILE
show( ggplot(mpg_df, aes('cty', 'hwy')) + geom_tile() )
# RASTER
show( ggplot(random_df, aes('x', 'y')) + geom_raster() )
# ERROR_BAR
show(
ggplot(class_df, aes(x='class')) + geom_errorbar(aes(ymin='min', ymax='max'))
)
# CROSSBAR
show(
ggplot(class_df, aes(x='class')) + geom_crossbar(aes(ymin='min', middle='median', ymax='max'))
)
# LINERANGE
show( ggplot(class_df, aes(x='class')) + geom_linerange(aes(ymin='min', ymax='max')) )
# POINTRANGE
show(
ggplot(class_df, aes(x='class')) + geom_pointrange(aes(ymin='min', y='median', ymax='max'))
)
# BOXPLOT
show( ggplot(mpg_df, aes('class', 'hwy')) + geom_boxplot() )
# VIOLIN
show( ggplot(mpg_df, aes('class', 'hwy')) + geom_violin() )
# AREA_RIDGE
show( ggplot(mpg_df, aes('hwy', 'class')) + geom_area_ridges() )
# AREA
show( ggplot(mpg_df, aes(x='hwy')) + geom_area(stat='bin') )
# CONTOURF
show( ggplot(random_df, aes('x', 'y')) + geom_contourf(aes(z='z')) )
# POLYGON
show( ggplot() + geom_polygon(data=states_df) )
# RECT
show( ggplot() + geom_rect(xmin=-1, xmax=1, ymin=-1, ymax=1) )
# RIBBON
n = 10
np.random.seed(42)
x = np.arange(n)
ymin = np.random.randint(-5, 0, size=n)
ymax = np.random.randint(1, 6, size=n)
show(
ggplot({'x': x, 'ymin': ymin, 'ymax': ymax}, aes(x='x')) + geom_ribbon(aes(ymin='ymin', ymax='ymax'))
)
# DENSITY2DF
show( ggplot(mpg_df, aes('cty', 'hwy')) + geom_density2df() )
# MAP
show( ggplot() + geom_map(data=states_df) )
# TEXT
show( ggplot() + geom_text(x=0, y=0, label='Lorem ipsum'))
#ggplot(mpg_df, aes('cty', 'hwy')) + geom_text(aes(label='fl'))
# LABEL
show( ggplot() + geom_label(x=0, y=0, label='Lorem ipsum') )
#ggplot(mpg_df, aes('cty', 'hwy')) + geom_label(aes(label='fl'))
# QQ
show( ggplot(mpg_df, aes(sample='hwy')) + geom_qq() )
# QQ2
show( ggplot(mpg_df, aes(x='cty', y='hwy')) + geom_qq2() )
# QQ_LINE
show( ggplot(mpg_df, aes(sample='hwy')) + geom_qq_line() )
# QQ2_LINE
show( ggplot(mpg_df, aes(x='cty', y='hwy')) + geom_qq2_line() )