Introduction
Set-up Environment
mamba create -n da python=3.11
mamba activate da
pip install seaborn matplotlib pandas yfinance numpy
Coding
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import yfinance as yf
import matplotlib.patheffects as path_effects
companies = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA']
start_date = '2020-01-01'
end_date = '2024-12-20'
stock_data = yf.download(companies, start=start_date, end=end_date)['Close']
stock_data.head()
stock_data.reset_index(inplace=True)
stock_data = stock_data.melt(id_vars='Date', var_name='Company', value_name='Close')
stock_data.head()
plt.figure(figsize=(6, 6))
sns.lineplot(data=stock_data, x='Date', y='Close', hue='Company')
custom_colors = {
'AAPL': '#6929c4', 'MSFT': '#1192e8', 'GOOGL': '#005d5d',
'AMZN': '#9f1853', 'TSLA': '#fa4d56'
}
plt.figure(figsize=(6, 6))
sns.lineplot(
data=stock_data,
x='Date',
y='Close',
hue='Company',
palette=custom_colors
)
plt.rcParams.update({
'axes.facecolor': '#000000', # Plot background
'axes.edgecolor': 'white', # Border color
'axes.labelcolor': 'white', # Labels color
'xtick.color': 'white', # X-axis tick color
'ytick.color': 'white', # Y-axis tick color
'grid.color': '#444444', # Gridline color
'text.color': 'white', # Text color
'figure.facecolor': '#000000', # Overall figure background
'figure.edgecolor': 'white', # Figure border color
})
plt.figure(figsize=(6, 6))
sns.lineplot(
data=stock_data,
x='Date',
y='Close',
hue='Company',
palette=custom_colors,
linewidth=2
)
plt.savefig("./stock_price.jpg", dpi=300)