<jupyter_start><jupyter_text>This notebook demonstrates how to use matplotlib and seaborn to build line charts involving major/ minor ticks, markers and data labels for the last point. It retrieve data tables from SingStat Table Builder using the [API function](https://www.tablebuilder.singstat.gov.sg/publicfacing/initApiList.action).   Example 1<jupyter_code>import json
import urllib.request
urlData = "https://www.tablebuilder.singstat.gov.sg/publicfacing/api/json/title/15144.json"

webURL = urllib.request.urlopen(urlData)
data = webURL.read()
#print(data)
df = json.loads(data)
# note that there are two different kinds of data tables/ structures
# df
from pandas.io.json import json_normalize
# population by age group for year 2000 to 2019
json_normalize(df['Level1'])
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df_new = json_normalize(df['Level1'])
df_new.columns = ['Type','Quarter','Rate']
df_new.dtypes
len(list(range(1992, 2020)))
range(28)
df_new['Rate'] = df_new['Rate'].astype(float)
list(df_new['Type'].unique())
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
                               AutoMinorLocator)
fig, ax = plt.subplots(figsize=(20,9))

#g = sns.lineplot(data = data, x='Quarter',y='Rate', hue='Type')
x = df_new[df_new['Type']==list(df_new['Type'].unique())[0]]['Quarter']
y = df_new[df_new['Type']==list(df_new['Type'].unique())[0]]['Rate']

ax.plot(x, y, 'o',  ls='-', markevery=4) 
plt.xticks(rotation=45)
plt.ylim(bottom=0)

ax.xaxis.set_major_locator(MultipleLocator(4))
#ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))

# For the minor ticks, use no labels; default NullFormatter.
ax.xaxis.set_minor_locator(MultipleLocator(1))
ax.yaxis.set_minor_locator(MultipleLocator(0.1))

plt.title('Total Unemployment Rate in Singapore Since 1992', fontsize=14)
plt.savefig('plotunemp.png', dpi=300, bbox_inches='tight')

fig, ax = plt.subplots(figsize=(20,9))

#g = sns.lineplot(data = data, x='Quarter',y='Rate', hue='Type')
x = df_new[df_new['Type']==list(df_new['Type'].unique())[2]]['Quarter']
y = df_new[df_new['Type']==list(df_new['Type'].unique())[2]]['Rate']

ax.plot(x, y, 'o',  ls='-', markevery=4, color='green')
plt.xticks(rotation=45)
plt.ylim(bottom=0)

ax.xaxis.set_major_locator(MultipleLocator(4))
#ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))

# For the minor ticks, use no labels; default NullFormatter.
ax.xaxis.set_minor_locator(MultipleLocator(1))
ax.yaxis.set_minor_locator(MultipleLocator(0.1))

plt.title('Citizen Unemployment Rate in Singapore Since 1992', fontsize=14)
#plt.savefig('plotunemp.png', dpi=300, bbox_inches='tight')<jupyter_output><empty_output><jupyter_text>Example 2<jupyter_code>urlData = "https://www.tablebuilder.singstat.gov.sg/publicfacing/api/json/title/14603.json"

webURL = urllib.request.urlopen(urlData)
data = webURL.read()
df = json.loads(data)
df_new = json_normalize(df['Level1'])
df_new.tail(10)
df_new.dtypes
df_new['value'] = pd.to_numeric(df_new['value'], errors="coerce")
df_new.tail()
df_new['year'] = df_new['year'].astype(int)
df_new.columns=['Percentile','Income','Year']
# plt.figure(figsize=(10,6))
fig, ax = plt.subplots(figsize=(10,6))
sns.lineplot(data=df_new, x='Year',y='Income',hue='Percentile')
plt.ylim(bottom=0)
plt.xlim(df_new['Year'].min()-1, df_new['Year'].max()+1)
ax.xaxis.set_major_locator(MultipleLocator(2))
ax.yaxis.set_minor_locator(MultipleLocator(100))

text1=df_new[(df_new['Year']==2019) & (df_new['Percentile']=='Median (50th Percentile)')]['Income'].astype(int).to_string(index=False)
plt.text(df_new[(df_new['Year']==2019) & (df_new['Percentile']=='Median (50th Percentile)')]['Year'], 
            df_new[(df_new['Year']==2019) & (df_new['Percentile']=='Median (50th Percentile)')]['Income'], text1)

text2=df_new[(df_new['Year']==2018) & (df_new['Percentile']=='20th Percentile')]['Income'].astype(int).to_string(index=False)
plt.text(df_new[(df_new['Year']==2018) & (df_new['Percentile']=='20th Percentile')]['Year'], 
            df_new[(df_new['Year']==2018) & (df_new['Percentile']=='20th Percentile')]['Income'], text2)

plt.title('Gross Monthly Income From Work (Including Employer CPF) Of Full-time Employed Residents, Annual', fontsize=12)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18245125348189414, 'fraction_numerical': 0.031104921077065924, 'mean_word_length': 7.566600397614314, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '414864', 'license': 'MIT', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1602, 'n_words': 266, 'path': 'notebooks/linecharts.ipynb', 'repo_name': 'huishun98/datakueh', 'size': '4308'}