#!/usr/bin/env python # coding: utf-8 # # Guided Project: Storytelling Data Visualization on Exchange Rates # # # Introducing the Dataset # In[1]: import pandas as pd exchange_rates = pd.read_csv('euro-daily-hist_1999_2020.csv') exchange_rates.head() # In[2]: exchange_rates.tail() # In[3]: exchange_rates.info() # # Data Cleaning # In[4]: exchange_rates.rename(columns={'[US dollar ]': 'US_dollar', '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) exchange_rates.head() # In[5]: euro_to_dollar = exchange_rates[['Time', 'US_dollar']].copy() euro_to_dollar['US_dollar'].value_counts() # 62 '-' characters # In[6]: 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() # # Rolling Mean # In[7]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') plt.plot(euro_to_dollar['Time'], euro_to_dollar['US_dollar']) plt.show() # In[8]: 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-adjusts the padding between subplots plt.show() # In[13]: euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar'].rolling(30).mean() euro_to_dollar # # Coming Up With an Idea # Here are a few story ideas for our data: # # * We show how the euro-dollar rate has changed during the coronavirus pandemic. We can show the 2020 data and the 2016-2019 data as a baseline. We can use a line plot. # * We show how the euro-dollar rate changed during the 2007-2008's financial crisis. We can also show the data for 2016 and 2009 for comparison. We can use a line plot. # * We show comparatively how the euro-dollar rate changed under the last three US presidents (George W. Bush (2001-2009), Barack Obama (2009-2017), and Donald Trump (2017-2021)). We can use a line plot. # # # # Storytelling Data Visualization Examples # # # Financial Crisis Example # In[14]: financial_crisis = euro_to_dollar.copy( )[(euro_to_dollar['Time'].dt.year >= 2006 ) & (euro_to_dollar['Time'].dt.year <= 2009)] financial_crisis_7_8 = euro_to_dollar.copy( )[(euro_to_dollar.Time.dt.year >= 2007 ) & (euro_to_dollar.Time.dt.year <= 2008)] # In[15]: ### Adding the FiveThirtyEight style import matplotlib.style as style style.use('fivethirtyeight') ### Adding the plot fig,ax = plt.subplots(figsize=(8,3)) ax.plot(financial_crisis['Time'], financial_crisis['rolling_mean'], linewidth=1, color='#A6D785') ### Highlighting the 2007-2008 period ax.plot(financial_crisis_7_8['Time'], financial_crisis_7_8['rolling_mean'], linewidth=3, color='#e23d28') ### Highlihting the peak of the crisis ax.axvspan(xmin=733112.0, xmax=733302.0, ymin=0.09, alpha=0.3, color='grey') ### Adding separate tick labels ax.set_xticklabels([]) ax.set_yticklabels([]) x = 732272.0 for year in ['2006', '2007', '2008', '2009', '2010']: ax.text(x, 1.13, year, alpha=0.5, fontsize=11) x += 365 y = 1.193 for rate in ['1.2', '1.3', '1.4', '1.5']: ax.text(732172.0, y, rate, alpha=0.5, fontsize=11) y += 0.1 ## Adding a title and a subtitle ax.text(732172.0, 1.67, "Euro-USD rate peaked at 1.59 during 2007-2008's financial crisis", weight='bold') ax.text(732172.0, 1.63, 'Euro-USD exchange rates between 2006 and 2010', size=12) ### Adding a signature ax.text(732172.0, 1.07, '©DATAQUEST' + ' '*94 + 'Source: European Central Bank', color = '#f0f0f0', backgroundcolor = '#4d4d4d', size=10) ### Add some transparency to the grid ax.grid(alpha=0.5) plt.show() # # The Three US Presidencies Example # In[16]: bush_obama_trump = euro_to_dollar.copy( )[(euro_to_dollar['Time'].dt.year >= 2001) & (euro_to_dollar['Time'].dt.year < 2021)] bush = bush_obama_trump.copy( )[bush_obama_trump['Time'].dt.year < 2009] obama = bush_obama_trump.copy( )[(bush_obama_trump['Time'].dt.year >= 2009) & (bush_obama_trump['Time'].dt.year < 2017)] trump = bush_obama_trump.copy( )[(bush_obama_trump['Time'].dt.year >= 2017) & (bush_obama_trump['Time'].dt.year < 2021)] # Below, you'll notice we used matplotlib's functional approach to build the graphs. We use this approach because it offers more flexibility in arranging the subplots: # # * We first build three of the graphs on a 2-by-3 grid (this grid should have six subplots, but we only build three; the bottom row remains empty). # * We then build only the bottom graph of a 2-by-1 grid (this grid should have two subplots; the top row remains empty). # * The two grids are merged, and we end up with three graphs on the top row and one graph on the bottom row. # In[17]: ### Adding the FiveThirtyEight style style.use('fivethirtyeight') ### Adding the subplots plt.figure(figsize=(12, 6)) ax1 = plt.subplot(2,3,1) ax2 = plt.subplot(2,3,2) ax3 = plt.subplot(2,3,3) ax4 = plt.subplot(2,1,2) axes = [ax1, ax2, ax3, ax4] ### Changes to all the subplots for ax in axes: ax.set_ylim(0.8, 1.7) ax.set_yticks([1.0, 1.2, 1.4, 1.6]) ax.set_yticklabels(['1.0', '1.2','1.4', '1.6'], alpha=0.3) ax.grid(alpha=0.5) ### Ax1: Bush ax1.plot(bush['Time'], bush['rolling_mean'], color='#BF5FFF') ax1.set_xticklabels(['', '2001', '', '2003', '', '2005', '', '2007', '', '2009'], alpha=0.3) ax1.text(731516.0, 1.92, 'BUSH', fontsize=18, weight='bold', color='#BF5FFF') ax1.text(731216.0, 1.8, '(2001-2009)', weight='bold', alpha=0.3) ### Ax2: Obama ax2.plot(obama['Time'], obama['rolling_mean'], color='#ffa500') ax2.set_xticklabels(['', '2009', '', '2011', '', '2013', '', '2015', '', '2017'], alpha=0.3) ax2.text(734288.0, 1.92, 'OBAMA', fontsize=18, weight='bold', color='#ffa500') ax2.text(734138.0, 1.8, '(2009-2017)', weight='bold', alpha=0.3) ### Ax3: Trump ax3.plot(trump['Time'], trump['rolling_mean'], color='#00B2EE') ax3.set_xticklabels(['2017', '', '2018', '', '2019', '', '2020', '', '2021'], alpha=0.3) ax3.text(736855.0, 1.92, 'TRUMP', fontsize=18, weight='bold', color='#00B2EE') ax3.text(736745.0, 1.8, '(2017-2021)', weight='bold', alpha=0.3) ### Ax4: Bush-Obama-Trump ax4.plot(bush['Time'], bush['rolling_mean'], color='#BF5FFF') ax4.plot(obama['Time'], obama['rolling_mean'], color='#ffa500') ax4.plot(trump['Time'], trump['rolling_mean'], color='#00B2EE') ax4.grid(alpha=0.5) ax4.set_xticks([]) ### Adding a title and a subtitle ax1.text(730016.0, 2.35, 'EURO-USD rate averaged 1.22 under the last three US presidents', fontsize=20, weight='bold') ax1.text(730016.0, 2.14, '''EURO-USD exchange rates under George W. Bush (2001 - 2009), Barack Obama (2009-2017), and Donald Trump (2017-2021)''', fontsize=16) ### Adding a signature ax4.text(729916.0, 0.65, '©DATAQUEST' + ' '*103 + 'Source: European Central Bank', color = '#f0f0f0', backgroundcolor = '#4d4d4d', size=14) plt.show()