阳光透过窗帘洒在宽敞的书房内,老爸坐在书桌前,眼前铺开的是各大上市公司的股票数据。他皱着眉头,眼前的数据繁杂而混乱,似乎隐藏着股市的某些秘密,等待着被揭晓。
老爸已在股市中浸淫多年,他深知数据的重要性,也明白在这些数字背后,隐藏着股票的价值和风险。这一次,他打算尝试一种新的方法来洞察这个复杂的市场。

他打开了笔记本电脑,启动了我为他设计的ChatGPT界面。在屏幕上,他轻轻敲下了这样一段话:“ 上传文件每个sheet分别是对应代码公司的历史股票数据,请首先可视化出每个公司的股票走势图,并附上代码和可视化图片。”
同时,老爸上传了一份包含多家公司股票数据的Excel文件。随着他敲下的回车键,ChatGPT生成了一段Python代码,并附上了一份指导:“下面的代码将帮助您可视化每家公司的股价走势,您可以通过图表清晰地看到每支股票的表现。”
# Re-importing libraries and re-loading the data due to the session reset
import pandas as pd
import matplotlib.pyplot as plt
# Re-loading the Excel file
xls = pd.ExcelFile('/mnt/data/stock_data.xlsx')
# Get the sheet names
sheet_names = xls.sheet_names
# Initialize a dictionary to store dataframes for each sheet
dfs = {}
# Loop through each sheet name and read the corresponding dataframe
for sheet_name in sheet_names:
dfs[sheet_name] = pd.read_excel(xls, sheet_name)
# Adjusting the code to use the correct column names and re-trying the visualization
# Create subplots
fig, axes = plt.subplots(len(sheet_names), 1, figsize=(10, 5*len(sheet_names)))
# Loop through each dataframe and plot the stock trend
for ax, (sheet_name, df) in zip(axes, dfs.items()):
ax.plot(df['date'], df['4. close'], label='Close Price')
ax.set_title(f'Stock Trend for {sheet_name}')
ax.set_xlabel('Date')
ax.set_ylabel('Close Price')
ax.legend()
# Adjust the layout
plt.tight_layout()
plt.savefig("/mnt/data/stock_trends.png")
plt.show()
老爸按照指导运行了代码,一张张股价走势图呈现在屏幕前,让复杂的数据变得直观易懂。他仔细分析了这些图表,尝试找出其中的规律和机会。


接着,他又输入:“我希望进一步分析这些公司的股价分布,找出哪些公司的股价波动较小,哪些较大。”
ChatGPT生成了第二段代码:“下面的代码将展示一个箱形图,用以展现各公司股价的分布和波动情况。”
import matplotlib.pyplot as plt
import pandas as pd
# Load the Excel file
xls = pd.ExcelFile("/mnt/data/stock_data.xlsx")
sheet_names = xls.sheet_names # get sheet names
# Store each sheet in the dictionary dfs
dfs = {sheet_name: xls.parse(sheet_name) for sheet_name in sheet_names}
# Calculating the percentage change in closing price for each stock
price_changes = {}
for sheet_name, df in dfs.items():
price_changes[sheet_name] = (df['4. close'].iloc[-1] - df['4. close'].iloc[0]) / df['4. close'].iloc[0] * 100
# Visualizing the percentage change using a bar chart
plt.figure(figsize=(10, 6))
plt.bar(price_changes.keys(), price_changes.values(), color='skyblue')
plt.xticks(rotation=45)
plt.ylabel('Percentage Change (%)')
plt.title('Stock Price Percentage Change Over the Period')
plt.tight_layout()
plt.savefig("/mnt/data/price_changes.png")
plt.show()
# Creating a DataFrame for boxplot
boxplot_data = pd.DataFrame({name: df['4. close'] for name, df in dfs.items()})
# Visualizing the stock prices using a boxplot
plt.figure(figsize=(10, 6))
boxplot_data.boxplot()
plt.xticks(rotation=45)
plt.ylabel('Close Price')
plt.title('Stock Price Distributions Over the Period')
plt.tight_layout()
plt.savefig("/mnt/data/stock_price_distributions.png")
plt.show()
老爸再次依葫芦画瓢,箱形图呈现了每家公司股价的分布和波动。老爸发现一些公司股价波动较小,似乎更加稳定,而一些公司波动较大,投资风险也随之增加。

在ChatGPT的协助下,老爸不仅深入分析了数据,还学习了如何用Python进行数据可视化,而这一切都来自于他与ChatGPT的简单对话。他感叹道:“看来,这个小助手不仅可以帮我分析数据,还能教我学编程呢!”
在这个过程中,老爸感受到了技术的力量,他实现了从数据到信息,再从信息到知识的转变。而我站在一旁,看着他脸上满意的笑容,心中暗自庆幸,这个小小的AI实现了它的价值——让技术为人服务,让数据分析变得触手可及。
可视化代码请关注公众号后,输入“AI数据分析获取”。