1105_4.10 製作一個 MP4
0:00
/0:20
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.dates import DateFormatter, date2num, num2date
import numpy as np
# Read the CSV file
input_file = '/content/drive/My Drive/TKU_Industry_Analysis_Semiconductor/Dataset_SEMI/semiconductor_merged_1016.csv'
df = pd.read_csv(input_file)
# Define company abbreviations
company_abbr = {
'ASML Holding N.V.': 'ASML',
'Advanced Micro Devices, Inc.': 'AMD',
'Analog Devices, Inc.': 'ADI',
'Applied Materials, Inc.': 'AMAT',
'Arm Holdings plc': 'ARM',
'Broadcom Inc.': 'AVGO',
'Intel Corporation': 'INTC',
'KLA Corporation': 'KLA',
'Lam Research Corporation': 'LRCX',
'Marvell Technology, Inc.': 'MRVL',
'MediaTek Inc.': 'MTK',
'Micron Technology, Inc.': 'MU',
'NVIDIA Corporation': 'NVDA',
'QUALCOMM Incorporated': 'QCOM',
'SK hynix Inc.': 'SKH',
'Samsung Electronics Co., Ltd.': 'SEC',
'Synopsys, Inc.': 'SNPS',
'Taiwan Semiconductor Manufacturing Company Limited': 'TSMC',
'Texas Instruments Incorporated': 'TXN',
'Tokyo Electron Limited': 'TEL'
}
# Replace company names with abbreviations
df['Company'] = df['Company Name'].replace(company_abbr)
# Ensure 'Date' column is in datetime format
df['Date'] = pd.to_datetime(df['Date'])
# Filter data from January 1, 2024
df = df[df['Date'] >= '2024-01-01']
# Calculate daily closing price percentage change
df.loc[:, 'Close_Price_Change%'] = df.groupby('Company Name')['Close Price_Local'].pct_change() * 100
# Filter required columns
df_filtered = df[['Date', 'Company', 'Close_Price_Change%']].dropna()
# Define highlighted companies with their colors and line styles
highlighted_companies = {
'NVDA': {'color': 'red', 'linewidth': 2},
'TSMC': {'color': 'blue', 'linewidth': 2},
'AMAT': {'color': 'lime', 'linewidth': 2},
'ASML': {'color': 'darkorchid', 'linewidth': 2},
'SEC': {'color': 'lightseagreen', 'linewidth': 2},
}
# Create the animation plot
fig, ax = plt.subplots(figsize=(10, 6))
# Set y-axis limits
ax.set_ylim(-15, 15)
ax.set_xlabel('Date')
ax.set_ylabel('Close Price Change %')
# Set date formatting
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
# Rotate x-axis labels
plt.xticks(rotation=90)
# Initialize lines for each company
lines = {}
for company in df_filtered['Company'].unique():
if company in highlighted_companies:
# Set specific company colors and line styles
lines[company] = ax.plot([], [], label=company, color=highlighted_companies[company]['color'],
linewidth=highlighted_companies[company]['linewidth'])[0]
else:
# Set remaining companies with low opacity
lines[company] = ax.plot([], [], label=company, color='gray', alpha=0.1, linewidth=1)[0]
# Set legend
ax.legend()
# Update function for each frame
def update(frame):
current_date = df_filtered['Date'].unique()[frame]
filtered_data = df_filtered[df_filtered['Date'] <= current_date]
for company in lines.keys():
company_data = filtered_data[filtered_data['Company'] == company]
lines[company].set_data(company_data['Date'], company_data['Close_Price_Change%'])
ax.set_title(f"Date: {current_date.strftime('%Y-%m-%d')}")
ax.relim() # Recalculate limits
ax.autoscale_view() # Update view limits
# Update regression lines for NVDA and AMAT
for company, color, linestyle in zip(['NVDA', 'AMAT'], ['black', 'gray'], ['-', '--']):
company_data = df_filtered[df_filtered['Company'] == company]
if not company_data.empty:
x = date2num(company_data['Date'])
y = company_data['Close_Price_Change%']
coefficients = np.polyfit(x, y, 2) # Fit a second-degree polynomial
polynomial = np.poly1d(coefficients)
# Generate points for the regression line
x_reg = np.linspace(x.min(), x.max(), 100)
y_reg = polynomial(x_reg)
# Plot the regression line
ax.plot(num2date(x_reg), y_reg, color=color, linestyle=linestyle, linewidth=2)
# Create the animation
ani = FuncAnimation(fig, update, frames=len(df_filtered['Date'].unique()), repeat=False, interval=100)
# Save as MP4
output_file = '/content/drive/My Drive/TKU_Industry_Analysis_Semiconductor/Dataset_SEMI/testing_3.mp4'
ani.save(output_file, writer='ffmpeg', fps=10)
plt.close()