<jupyter_start><jupyter_text>Templot Examples We start by installing the package from Pypi, this is not necessary if the templot folder is present in the same diretory as this notebook.<jupyter_code>!pip install --user templot<jupyter_output><empty_output><jupyter_text>Importing dependencies<jupyter_code>%matplotlib inline
import os
import pandas as pd
import matplotlib.animation as animation
import matplotlib.pyplot as plt 
from IPython.display import HTML #in order to visualize animations<jupyter_output><empty_output><jupyter_text>Data Downloading and preprocessingWe can use the `download_irep` function to download and parse the IREP dataset.<jupyter_code>from templot import download_irep
filepath = os.path.join('.', 'templot', 'data', 'df.csv')
if not os.path.exists(filepath):
    download_irep(filepath)

df = pd.read_csv(filepath)
df.head()<jupyter_output><empty_output><jupyter_text>We can use the `add_regions` function that adds the corresponding Region, Department, or/and Commune by looking at the longitude and latitude columns.<jupyter_code>from templot import add_regions

df = add_regions(df, "LLX", "LLY", add=["regions", "departements"]) 

df.head()<jupyter_output><empty_output><jupyter_text>Some of the functions require the dataset to be in a melted form; i.e. having one columns containing the values and another containing the corresponding year.<jupyter_code>df_melted = pd.melt(
    df,
    id_vars=df.columns & [
                'Identifiant',
                'Nom_Etablissement_x',
                'LLX',
                'LLY',
                'Regions',
                'Departements',
                'Communes'
            ],
    var_name='Annee',
    value_name='Quantite',
)
df_melted = df_melted[df_melted.Quantite != 0]
df_melted['Annee'] = df_melted['Annee'].apply(lambda x: int(x[-4:]))
df_melted.head()<jupyter_output><empty_output><jupyter_text>Example 1: Plot Aggregated MapHere we can create a map that shows the evolution of the average quantity in every region. The color refelects the average cummulative quantity over the entire period.<jupyter_code>from templot import plot_aggregated_map
my_map = plot_aggregated_map(
    data=df,
    variables=[
        "Quantite2004",
        "Quantite2005",
        "Quantite2006",
        "Quantite2007",
        "Quantite2008",
        "Quantite2009",
    ],
    group="Regions",
    aggregation_method="average",
    height=300)

my_map
#If the map is not displayed (if you're using Edge or Chrome) uncomment the follwing lines
# from IPython.display import IFrame
# my_map.save("map.html")
# IFrame("map.html", width='100%', height=750)<jupyter_output><empty_output><jupyter_text>We can also look at the evolution of the number of polluting comapnies in each department. The function will warn us if the number of departments is too high.<jupyter_code>my_map_dep = plot_aggregated_map(
    data=df,
    variables=[col for col in df.columns if "Quantite" in col], # All years
    group="Departements",
    aggregation_method="count",
    height=300)

my_map_dep
#If the map is not displayed (if you're using Edge or Chrome) uncomment the follwing lines
# from IPython.display import IFrame
# my_map_dep.save("map_dep.html")
# IFrame("map_dep.html", width='100%', height=750)<jupyter_output><empty_output><jupyter_text>Example 2: Plot Polar Bar Evolution Here we can create an animation that shows the evolution of the maximum quantities per region across all years.<jupyter_code>from templot import plot_polar_bar_evolution

anim = plot_polar_bar_evolution(
    df_melted,
    var="Quantite",
    year="Annee",
    agr="max",
    y_grid=False,
    x_grid=False,
    y_ticks=False,
)

HTML(anim.to_jshtml())<jupyter_output><empty_output><jupyter_text>Example 3: Plot Polar Bar Evolution InteractiveWe can also look at an interactive version of that graph.<jupyter_code>from templot import plot_polar_bar_evolution_interactive

fig = plot_polar_bar_evolution_interactive(df_melted,
    var="Quantite",
    year="Annee",
    agr="max")

fig<jupyter_output><empty_output><jupyter_text>Example 4: Plot Pie Chart InteractiveHere we can look closely at two specific years and compare between them.<jupyter_code>from templot import plot_pie_chart_interactive


fig = plot_pie_chart_interactive(df, "Quantite", 2004, 2005, "Regions")

fig<jupyter_output><empty_output><jupyter_text>Example 5: Plot Top 10 BarchartFinally we take a look at the top 10 worst offenders every year.<jupyter_code>from templot import plot_top10_barchart

# Delete a few outliers/incorrect values
df_melted.drop(df_melted.index[[80307, 78095, 73504]], inplace=True)

# Plot animation from 2003 to 2017 :
fig, ax = plt.subplots(figsize=(16, 9), dpi=220, facecolor='#F8F7F7')
ani = animation.FuncAnimation(
    fig,
    plot_top10_barchart,
    frames=range(2003, 2018),
    interval=1500,
    fargs=[
        df_melted,
        "Quantite",
        "Annee",
        "Regions",
        'Nom_Etablissement_x',
        'Les établissement émettant le plus de déchets dangereux de 2003 à 2017',
        'Déchets dangereux (t/an)',
    ],
)
HTML(ani.to_jshtml())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09424795958025652, 'fraction_numerical': 0.022736105713175282, 'mean_word_length': 4.612868047982552, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '442950', 'license': 'MIT', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1659, 'n_words': 461, 'path': 'example.ipynb', 'repo_name': 'khllkcm/templot', 'size': '5146'}