#!/usr/bin/env python
# coding: utf-8
# You can get this as Jupyter notebook at https://github.com/janbrus/ssb-api-python-examples

# # Get CSV form Statistics Norway's API to Python Pandas
# ## Economic trends - forecasts
# ### Get dataset  [Selected Forecasts 4 years](https://data.ssb.no/api/v0/dataset/934516) as CSV  from Statistics Norway's [https://data.ssb.no/api/v0/dataset?lang=en](API: Ready-made datasets)

# Import Python pandas to make a dataframe
import pandas as pd


# pd.read_csv() gets the content from the API-et and put it in a dataframe df.
# CSV in this API is UTF-8, we have to give encoding parameter to get æøå right.
df = pd.read_csv("https://data.ssb.no/api/v0/dataset/934516.csv?lang=en", encoding = "ISO-8859-1" )

# Check that datatype is Pandas dataframe
df.info()


# 12 first lines of the dataframe
df.head(12)


# Optional: Simple change to date format. Remove the commment when you understand what's happening.

#df['year'] = pd.to_datetime(df['year'], format='%Y', errors='coerce')


# Make line chart as Pandas plot, where we input x and y axis. Grouping by .groupby
df.groupby('contents').plot(x='year', y='12880: Main economic indicators. Accounts and forecasts, by year and contents', figsize=(12, 6), color ='g')


# In order to get all the lines in a Pandas plot the best is to pivot the table. W make a new dataframe df2, with year as index.
df2 = df.pivot(index='år', columns='contents',
               values='12880: Main economic indicators. Accounts and forecasts, by year and contents')

# df2 is 'wide' in stead of 'long'
df2

# Pandas plot of df2 with size marker and title
df2.plot(figsize=(16, 10), marker='v', title='Main economic indicators. Selected forecasts, 4 years')


# ### Plot using the 'up-and-coming' [Plotly](https://plotly.com/python/). Here I use the simpler Plotly express.

import plotly.express as px  # px is standard for Plotly express. Has nothing to do with StatBank px

fig = px.line(df, x='year', y='12880: Main economic indicators. Accounts and forecasts, by year and contents')

# Input here is the original df, which is 'long'. Withoput grouping of contents, the output is bad.

fig.show()

# Plotly Express has the argumentet 'line_group', for grouping. Here I also give the title and labels for the x and y axis.
px.line(df, x=('år'), y='12880: Main economic indicators. Accounts and forecasts, by year and contents',
              color='contents',
              line_group='contents',
              title='Main economic indicators. Forecasts, 4 years',
              labels={'12880: Main economic indicators. Accounts and forecasts, by year and contents':'Change / Levels'})


# ### Using the new version,  Plotly Express 4.8
# Plotly Express have got new functionality in the version 4.8, released 26. May 2020.
# It is now supporting 'wide' format. Now we can make the same plot with df2 which is 'wide'

df2.head(2)
fig2 = px.line(df2, x=df2.index, y=df2.columns, line_group='contents')
fig2.show()


# It is now possible to set Plotly as Pandas .plot bbackend instead of Matplotlib
pd.options.plotting.backend = "plotly"

# Then plot of df2 works without parameters
df2.plot()

# template parameter is not new
df2.plot.area(facet_col='contents', facet_col_wrap=2, template='none')

# A tip to the end: Try to make your own dataset instead. You can also use the richer format JSON-stat with the Python library pyjstat in preference to CSV.
