This Guided Project would revolve around explanatory data visualization by adopting Gestalt principles utilizing Matplotlib built-in styles, in this case 'FiveThirtyEight' style. We will be using Euro daily exchange rates between 1999 and 2021. The dataset was gathered by Daria Chemkaeva and was taken from Kaggle The data source is the European Central Bank and it gets regular updates. For this case the data was downloaded in January 2021.
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
exchange_rates = pd.read_csv('euro-daily-hist_1999_2020.csv')
exchange_rates.info()
exchange_rates.head()
Main focus is on the exchange rate between the euro and the American dollar.
exchange_rates.rename(columns={'[US dollar ]':'US_dollar','[Indonesian rupiah ]':'ID_rupiah',
'Period\\Unit:': 'Time'},
inplace=True)
exchange_rates['Time'] = pd.to_datetime(exchange_rates[
'Time'])
exchange_rates.sort_values('Time',inplace=True)
exchange_rates.reset_index(drop=True, inplace=True)
euro_to_dollar = exchange_rates[['Time','US_dollar']]
euro_to_dollar['US_dollar'].value_counts()
# Drop all the rows where the - character appears in the US_dollar column
euro_to_dollar=euro_to_dollar[euro_to_dollar['US_dollar'] != '-']
euro_to_dollar['US_dollar']= euro_to_dollar['US_dollar'].astype(float)
euro_to_dollar.info()
euro_to_dollar.head()
# Visualizing the evolution of the euto-dollar exchange rate
plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar'])
plt.show()
euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar']. rolling(30). mean()
euro_to_dollar
# Plotting different values of moving windows
plt.style.use('default')
plt.figure(figsize= (9,6))
plt.subplot(3,2,1)
plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar'])
plt.title('Original values', weight='bold')
for i, rolling_mean in zip([2,3,4,5,6], [7,30, 50, 100, 365]):
plt.subplot(3,2,i)
plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar']. rolling(rolling_mean).mean())
plt.title('Rolling Window:' + str(rolling_mean), weight='bold')
plt.tight_layout() # Auto-adjust the padding between subplots
plt.show()
Let's see how certain currency like Indonesian Rupiah which closely tied to USD affected during the sub-prime crisis leading to Globaal Financial Crisis (GFC) back in 2007-2009. So, I am going to use the dataset to find out how Indonesian Rupiah fare against EUR and USD particularly during the 2007-2009. We may refer to the background story here and from this source.
# We need to rename the Indonesian Rupiah column
euro_to_idr= exchange_rates[['Time', 'ID_rupiah']]
euro_to_idr.info()
euro_to_idr['ID_rupiah'].value_counts()
# Removing the 'IDR_rupiah'with value '-'
euro_to_idr=euro_to_idr. loc[euro_to_idr['ID_rupiah']!='-']
euro_to_idr['ID_rupiah'].value_counts()
#converting to float
euro_to_idr['ID_rupiah']=euro_to_idr['ID_rupiah'].astype(float)
euro_to_idr.info()
# Plotting idr rolling mean
euro_to_idr['rolling_mean']= euro_to_idr['ID_rupiah'].rolling(30).mean()
euro_to_idr.head()
idr_before_GFC=euro_to_idr.copy()[(euro_to_idr['Time'].dt.year>=2005) & (euro_to_idr['Time'].dt.year<=2008)]
idr_during_GFC = euro_to_idr.copy()[(euro_to_idr['Time'].dt.year>=2008)&(euro_to_idr['Time'].dt.year<=2010)]
idr_after_GFC=euro_to_idr.copy()[(euro_to_idr['Time'].dt.year>=2010)]
euro_to_idr['rolling_mean'] = euro_to_idr['ID_rupiah'].rolling(30).mean()
plt.plot(euro_to_idr['Time'], euro_to_idr['rolling_mean'])
plt.show()
import matplotlib.style as style
from matplotlib.patches import Circle
from datetime import datetime
style.use('fivethirtyeight')
fig, ax = plt.subplots(figsize=(8,5))
ax.plot(idr_before_GFC['Time'], idr_before_GFC['rolling_mean'], lw=2, color='b')
ax.plot(idr_during_GFC['Time'], idr_during_GFC['rolling_mean'], lw=2, color='red', label= 'Indonesia GFC Period')
ax.plot(idr_after_GFC['Time'], idr_after_GFC['rolling_mean'], lw=2, color='b')
# print('ax:', ax.get_xticks()); to determine coordinate for text on the span
# print('ax:', ax.get_yticks()); to determine coordinatte for title
fig.set_dpi(80)
ax.grid(alpha=0.2)
ax.text((datetime(2005,1,1)), 18000,' Indonesian Rupiah Against Euro', size=18, weight='bold')
ax.text((datetime(2005,1,1)),17500, 'During Global Financial Crisis (GFC) 2007-2009', size= 16)
ax.text(733030,15800, 'Indonesia\nGFC-Period', size=10)
ax.set_xlabel('Years', fontsize= 12)
ax.set_ylabel('Indonesian IDR against Euro', fontsize = 12)
ax.axvspan(xmin=733042, xmax= 733773, ymax = 0.7, alpha=0.3, color='grey')
plt.show()
fig, ax = plt.subplots(figsize=(8,5), dpi=80)
ax.plot(idr_before_GFC['Time'], idr_before_GFC['rolling_mean'], lw=2, color='b')
ax.plot(idr_during_GFC['Time'], idr_during_GFC['rolling_mean'], lw=2, color='red', label= 'Indonesia GFC Period')
ax.text(733030, 17500, 'Impact of GFC to IDR against Euro', fontsize=15, weight='bold')
ax.plot(idr_after_GFC['Time'], idr_after_GFC['rolling_mean'], lw=2, color='b')
ax.grid(alpha=0.2)
ax.set_xlabel('Years', fontsize= 12)
ax.set_ylabel('Indonesian IDR against Euro', fontsize = 12)
ax.text(733030,15800, 'Indonesia\nGFC-Period', size=10)
ax.axvspan(xmin=733042, xmax= 733773, ymax = 0.7, alpha=0.2, color='grey')
ax.text(735100, 16770, 'What happen\nin this period?', size = 10)
ax.axvspan(xmin=735150, xmax=735950, ymax=0.84, alpha=0.3, color='red')
plt.show()
idr_2014to2016=euro_to_idr.copy()[(euro_to_idr['Time'].dt.year>=2014)&(euro_to_idr['Time'].dt.year<=2016)]
dollar_2014to2016=euro_to_dollar.copy()[(euro_to_dollar['Time'].dt.year>=2014)&(euro_to_dollar['Time'].dt.year<=2016)]
plt.figure(figsize=(16,10),dpi=70)
ax1=plt.subplot(2,2,1)
ax2=plt.subplot(2,2,2)
axes=[ax1,ax2]
for ax in axes:
ax.grid(alpha=0.2)
ax.set_xlabel('Years', fontsize=12)
ax1.plot(idr_2014to2016['Time'],idr_2014to2016['rolling_mean'], label="IDR", color='blue', lw=2)
ax1.set_xticklabels(['','2014','', '','', '2015','','', '', '2016'])
ax1.text(735599, 16800,'IDR Response against Euro',fontsize=14, weight='bold', alpha=0.6 )
ax1.text(735599, 16600, 'Mirror USD responses', weight= 'bold', alpha=0.6)
# print('ax1:', ax1.get_xticks())
ax2.plot(dollar_2014to2016['Time'],dollar_2014to2016['rolling_mean'], label='USD', color='red', lw=2)
ax2.set_xticklabels(['','2014', '','','', '2015','', '','', '2016'])
ax2.text(735719, 1.42, 'USD Strengthening', fontsize=14, weight='bold', alpha=0.6)
ax2.text(735719, 1.40, 'With Tightening of QE', fontsize=12, weight='bold', alpha=0.6)
ax1.text(735150, 13200, '©DATAQUEST',color='k',size = 14, weight='bold', alpha=0.7)
ax2.text(735750,0.96, 'Source:European Central Bank',color= 'k', size=14, weight='bold', alpha=0.7)
# print('ax2:', ax2.get_xticks)
plt.show()