The charts below depict the historical retail sales and the year-over-year percent change in retail sales. Notably, the retail sales experienced a significant surge during the pandemic period, while the year-over-year percent change exhibited a rapid decline. It is worth mentioning that the latest value for the year-over-year percent change stands at 0.54%.
Python codes to generate -
import pandas as pd import pandas_datareader.data as web import matplotlib.pyplot as plt import matplotlib.dates as mdates import numpy as np # Define the date range start = pd.to_datetime('1992-01-01') end = pd.to_datetime('2023-04-30') # Download the retail sales data from FRED retail_sales = web.DataReader('RSXFS', 'fred', start, end) # Plot the data plt.figure(figsize=(10, 5)) plt.plot(retail_sales.index, retail_sales.values, label='Retail Sales') # Fit a trend line using linear regression x = np.arange(len(retail_sales)) y = retail_sales.values.flatten() coefficients = np.polyfit(x, y, 1) trend_line = np.polyval(coefficients, x) plt.plot(retail_sales.index, trend_line, color='red', linestyle='--', label='Trend Line') # Display the latest value latest_date = retail_sales.index[-1].strftime('%Y-%m-%d') latest_value = retail_sales.values[-1][0] latest_date_num = mdates.date2num(retail_sales.index[-1]) plt.annotate(f'Latest Value\n({latest_date}, {latest_value})', xy=(latest_date_num, latest_value), xytext=(latest_date_num, latest_value+100), arrowprops=dict(facecolor='black', arrowstyle='->')) # Configure x-axis date formatting date_fmt = mdates.DateFormatter('%Y-%m-%d') plt.gca().xaxis.set_major_formatter(date_fmt) plt.gca().xaxis.set_major_locator(mdates.YearLocator()) plt.title('Retail Sales from FRED') plt.ylabel('Retail Sales') plt.legend() plt.grid(True) # Rotate the x-axis label vertically plt.xticks(rotation='vertical') plt.show()
import pandas as pd import pandas_datareader.data as web import matplotlib.pyplot as plt # Define the date range start = pd.to_datetime('1993-01-01') end = pd.to_datetime('2023-04-30') # Download the retail sales data from FRED retail_sales = web.DataReader('RSXFS', 'fred', start, end) # Calculate the year-over-year percent change retail_sales_change = retail_sales.pct_change(periods=12) * 100 # Calculate the average average_change = retail_sales_change.mean()[0] # Extract the value from the Series # Plot the data plt.figure(figsize=(10,5)) plt.plot(retail_sales_change.index, retail_sales_change.values, label='Percent Change') plt.axhline(y=average_change, color='r', linestyle='-', label='Average') plt.title('Year-over-Year Percent Change in Retail Sales from FRED') plt.xlabel('Year') plt.ylabel('Percent Change') plt.legend() plt.grid(True) # Display the latest value latest_date = retail_sales_change.index[-1].strftime('%Y-%m-%d') latest_value = retail_sales_change.iloc[-1].values[0] print(f'The latest value on {latest_date} is {latest_value:.2f}%') plt.show()
No comments:
Post a Comment