# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [default]
#     language: python
#     name: python3
# ---

# Krisk is created for building statistical interactive visualization with pandas+Jupyter integration on top of Echarts.

import pandas as pd
import krisk.plot as kk
# Use this when you want to nbconvert the notebook (used by nbviewer)
from krisk import init_notebook; init_notebook()

# We will be using GapMinder data for examples below.

# +
df = pd.read_csv('http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/'
                              'examples/gapminder/data/'
                              'gapminderDataFiveYear.txt', sep='\t')

df.head()
# -

# Let's start by small example. Using bar plot to count the data of category,

kk.bar(df,'continent')

# Note that by default, the plot already used a tooltip. You can hover the plot to see the y-value.

# We also can plot bar by averaging GDP per capita for each continent,

kk.bar(df,'continent',y='gdpPercap',how='mean')

# We can change x as year, and use the grouping on continent,

kk.bar(df,'year',y='gdpPercap',c='continent',how='mean')

# Stacked and annotate the chart,

(kk.bar(df,'year',y='gdpPercap',c='continent',how='mean',stacked=True,annotate=True)
 .set_size(width=1000))

# Next we can do the same thing with line chart, using area, annotate, and tooltip based on axis,

p = kk.line(df,'year',y='gdpPercap',c='continent',how='mean',
           stacked=True,annotate='all',area=True)
p.set_tooltip_style(trigger='axis',axis_pointer='shadow')
p.set_size(width=1000)

# We can also create a histogram and add theme into it,

p = (kk.hist(df,x='lifeExp',c='continent',stacked=True,bins=100))
p.set_tooltip_style(trigger='axis',axis_pointer='shadow')
p.set_theme('vintage')

# Let's get a little bit advanced. We're going to create scatter points of GapMinder data in 2007. We use Life Expectancy, GDP per Capita, and Population as x,y,size respectively. We also want to add the information on the tooltip, add and reposition toolbox, legend, and title.

p = kk.scatter(df[df.year == 2007],'lifeExp','gdpPercap',s='pop',c='continent')
p.set_size(width=1000, height=500)
p.set_tooltip_format(['country','lifeExp','gdpPercap','pop','continent'])
p.set_theme('dark')
p.set_toolbox(save_format='png',restore=True,data_zoom=True)
p.set_legend(orient='vertical',x_pos='-1%',y_pos='-3%')
p.set_title('GapMinder of 2007',x_pos='center',y_pos='-5%')

# In the next few notebooks, we're going to dig deeper at each of the feature, including what's not being discussed here. But this introduction should give a sense of what krisk is capable of.

StarcoderdataJupyterScriptsDedupFiltered