Python股票数据可视化代码详解
来源:脚本之家    时间:2022-03-16 13:01:26
目录
数据准备阿里巴巴谷歌苹果腾讯亚马逊Facebook数据可视化查看各个公司的股价平均值查看各公司股价分布情况股价走势对比总结
import numpy as np
import pandas as pd
from pandas_datareader import data
import datetime as dt

数据准备

"""
获取国内股票数据的方式是:“股票代码”+“对应股市”(港股为.hk,A股为.ss)
例如腾讯是港股是:0700.hk
"""
#字典:6家公司的股票
# gafataDict={"谷歌":"GOOG","亚马逊":"AMZN","Facebook":"FB", "苹果":"AAPL","阿里巴巴":"BABA","腾讯":"0700.hk"}
"""
定义函数
函数功能:计算股票涨跌幅=(现在股价-买入价格)/买入价格
输入参数:column是收盘价这一列的数据
返回数据:涨跌幅
"""
def change(column):
    # 买入价格
    buyPrice=column[0]
    # 现在股价
    curPrice=column[column.size-1]
    priceChange=(curPrice-buyPrice)/buyPrice
    # 判断股票是上涨还是下跌
    if priceChange>0:
        print("股票累计上涨=",round(priceChange*100,2),"%")
    elif priceChange==0:
        print("股票无变化=",round(priceChange*100,2)*100,"%")
    else:
        print("股票累计下跌=",round(priceChange*100,2)*100,"%")
    # 返回数据
    return priceChange
"""
三星电子
每日股票价位信息
Open:开盘价
High:最高加
Low:最低价
Close:收盘价
Volume:成交量
因雅虎连接不到,仅以三星作为获取数据示例
"""
sxDf = data.DataReader("005930", "naver", start="2021-01-01", end="2022-01-01")
sxDf.head()
OpenHighLowCloseVolume
Date
2021-01-048100084400802008300038655276
2021-01-058160083900816008390035335669
2021-01-068330084500821008220042089013
2021-01-078280084200827008290032644642
2021-01-088330090000830008880059013307
sxDf.info()

DatetimeIndex: 248 entries, 2021-01-04 to 2021-12-30
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   Open    248 non-null    object
 1   High    248 non-null    object
 2   Low     248 non-null    object
 3   Close   248 non-null    object
 4   Volume  248 non-null    object
dtypes: object(5)
memory usage: 11.6+ KB
sxDf.iloc[:,0:4]=sxDf.iloc[:,0:4].astype("float")
sxDf.iloc[:,-1]=sxDf.iloc[:,-1].astype("int")
sxDf.info()
DatetimeIndex: 248 entries, 2021-01-04 to 2021-12-30Data columns (total 5 columns): #   Column  Non-Null Count  Dtype  ---  ------  --------------  -----   0   Open    248 non-null    float64 1   High    248 non-null    float64 2   Low     248 non-null    float64 3   Close   248 non-null    float64 4   Volume  248 non-null    int32  dtypes: float64(4), int32(1)memory usage: 10.7 KB
DatetimeIndex: 248 entries, 2021-01-04 to 2021-12-30
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Open    248 non-null    float64
 1   High    248 non-null    float64
 2   Low     248 non-null    float64
 3   Close   248 non-null    float64
 4   Volume  248 non-null    int32  
dtypes: float64(4), int32(1)
memory usage: 10.7 KB

阿里巴巴

# 读取数据
AliDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\阿里巴巴2017年股票数据.xlsx",index_col="Date")
AliDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-22175.839996176.660004175.039993176.289993176.28999312524700
2017-12-26174.550003175.149994171.729996172.330002172.33000212913800
2017-12-27172.289993173.869995171.729996172.970001172.97000110152300
2017-12-28173.039993173.529999171.669998172.300003172.3000039508100
2017-12-29172.279999173.669998171.199997172.429993172.4299939704600
# 查看基本信息及数据类型
AliDf.info()

DatetimeIndex: 251 entries, 2017-01-03 to 2017-12-29
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   Open       251 non-null    float64
 1   High       251 non-null    float64
 2   Low        251 non-null    float64
 3   Close      251 non-null    float64
 4   Adj Close  251 non-null    float64
 5   Volume     251 non-null    int64  
dtypes: float64(5), int64(1)
memory usage: 13.7 KB
# 计算涨跌幅
AliChange=change(AliDf["Close"])
股票累计上涨= 94.62 %
"""增加一列累计增长百分比"""
#一开始的股价
Close1=AliDf["Close"][0]
# # .apply(lambda x: format(x, ".2%"))
AliDf["sum_pct_change"]=AliDf["Close"].apply(lambda x: (x-Close1)/Close1)
AliDf["sum_pct_change"].tail()
Date
2017-12-22    0.989729
2017-12-26    0.945034
2017-12-27    0.952257
2017-12-28    0.944695
2017-12-29    0.946162
Name: sum_pct_change, dtype: float64

谷歌

# 读取数据
GoogleDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\谷歌2017年股票数据.xlsx",index_col="Date")
GoogleDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-221061.1099851064.1999511059.4399411060.1199951060.119995755100
2017-12-261058.0699461060.1199951050.1999511056.7399901056.739990760600
2017-12-271057.3900151058.3699951048.0500491049.3699951049.3699951271900
2017-12-281051.5999761054.7500001044.7700201048.1400151048.140015837100
2017-12-291046.7199711049.6999511044.9000241046.4000241046.400024887500
# 计算涨跌幅
GoogleChange=change(GoogleDf["Close"])
股票累计上涨= 33.11 %
"""增加一列累计增长百分比"""
#一开始的股价
Close1=GoogleDf["Close"][0]
# # .apply(lambda x: format(x, ".2%"))
GoogleDf["sum_pct_change"]=GoogleDf["Close"].apply(lambda x: (x-Close1)/Close1)
GoogleDf["sum_pct_change"].tail()
Date
2017-12-22    0.348513
2017-12-26    0.344213
2017-12-27    0.334839
2017-12-28    0.333274
2017-12-29    0.331061
Name: sum_pct_change, dtype: float64

苹果

# 读取数据
AppleDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\苹果2017年股票数据.xlsx",index_col="Date")
AppleDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-22174.679993175.419998174.500000175.009995174.29936216349400
2017-12-26170.800003171.470001169.679993170.570007169.87739633185500
2017-12-27170.100006170.779999169.710007170.600006169.90727221498200
2017-12-28171.000000171.850006170.479996171.080002170.38531516480200
2017-12-29170.520004170.589996169.220001169.229996168.54283125999900
# 计算涨跌幅
AppleChange=change(AppleDf["Close"])
股票累计上涨= 45.7 %
"""增加一列累计增长百分比"""
#一开始的股价
Close1=AppleDf["Close"][0]
# # .apply(lambda x: format(x, ".2%"))
AppleDf["sum_pct_change"]=AppleDf["Close"].apply(lambda x: (x-Close1)/Close1)
AppleDf["sum_pct_change"].tail()
Date
2017-12-22    0.506758
2017-12-26    0.468532
2017-12-27    0.468790
2017-12-28    0.472923
2017-12-29    0.456995
Name: sum_pct_change, dtype: float64

腾讯

# 读取数据
TencentDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\腾讯2017年股票数据.xlsx",index_col="Date")
TencentDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-22403.799988405.799988400.799988405.799988405.79998816146080
2017-12-27405.799988407.799988401.000000401.200012401.20001216680601
2017-12-28404.000000408.200012402.200012408.200012408.20001211662053
2017-12-29408.000000408.000000403.399994406.000000406.00000016601658
2018-01-02406.000000406.000000406.000000406.000000406.0000000
# 读取数据
TencentDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\腾讯2017年股票数据.xlsx",index_col="Date")
TencentDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-22403.799988405.799988400.799988405.799988405.79998816146080
2017-12-27405.799988407.799988401.000000401.200012401.20001216680601
2017-12-28404.000000408.200012402.200012408.200012408.20001211662053
2017-12-29408.000000408.000000403.399994406.000000406.00000016601658
2018-01-02406.000000406.000000406.000000406.000000406.0000000
# 计算涨跌幅
TencentChange=change(TencentDf["Close"])
股票累计上涨= 114.36 %
"""增加一列累计增长百分比"""
#一开始的股价
Close1=TencentDf["Close"][0]
# # .apply(lambda x: format(x, ".2%"))
TencentDf["sum_pct_change"]=TencentDf["Close"].apply(lambda x: (x-Close1)/Close1)
TencentDf["sum_pct_change"].tail()
Date
2017-12-22    1.142555
2017-12-27    1.118268
2017-12-28    1.155227
2017-12-29    1.143611
2018-01-02    1.143611
Name: sum_pct_change, dtype: float64

亚马逊

# 读取数据
AmazonDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\亚马逊2017年股票数据.xlsx",index_col="Date")
AmazonDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-221172.0799561174.6199951167.8299561168.3599851168.3599851585100
2017-12-261168.3599851178.3199461160.5500491176.7600101176.7600102005200
2017-12-271179.9100341187.2900391175.6099851182.2600101182.2600101867200
2017-12-281189.0000001190.0999761184.3800051186.0999761186.0999761841700
2017-12-291182.3499761184.0000001167.5000001169.4699711169.4699712688400
# 计算涨跌幅
AmazonChange=change(AmazonDf["Close"])
股票累计上涨= 55.17 %
"""增加一列累计增长百分比"""
#一开始的股价
Close1=AmazonDf["Close"][0]
# # .apply(lambda x: format(x, ".2%"))
AmazonDf["sum_pct_change"]=AmazonDf["Close"].apply(lambda x: (x-Close1)/Close1)
AmazonDf["sum_pct_change"].tail()
Date
2017-12-22    0.550228
2017-12-26    0.561373
2017-12-27    0.568671
2017-12-28    0.573766
2017-12-29    0.551700
Name: sum_pct_change, dtype: float64

Facebook

# 读取数据
FacebookDf=pd.read_excel(r"C:\Users\EDY\Desktop\吧哩吧啦\学习\Untitled Folder\Facebook2017年股票数据.xlsx",index_col="Date")
FacebookDf.tail()
OpenHighLowCloseAdj CloseVolume
Date
2017-12-22177.139999177.529999176.229996177.199997177.1999978509500
2017-12-26176.630005177.000000174.669998175.990005175.9900058897300
2017-12-27176.550003178.440002176.259995177.619995177.6199959496100
2017-12-28177.949997178.940002177.679993177.919998177.91999812220800
2017-12-29178.000000178.850006176.460007176.460007176.46000710261500
# 计算涨跌幅
FacebookChange=change(FacebookDf["Close"])
股票累计上涨= 51.0 %
"""增加一列每日增长百分比"""
# .pct_change()返回变化百分比,第一行因没有可对比的,返回Nan,填充为0
FacebookDf["pct_change"]=FacebookDf["Close"].pct_change(1).fillna(0)
FacebookDf["pct_change"].head()
Date
2017-01-03    0.000000
2017-01-04    0.015660
2017-01-05    0.016682
2017-01-06    0.022707
2017-01-09    0.012074
Name: pct_change, dtype: float64
"""增加一列累计增长百分比"""
#一开始的股价
Close1=FacebookDf["Close"][0]
# .apply(lambda x: format(x, ".2%"))
FacebookDf["sum_pct_change"]=FacebookDf["Close"].apply(lambda x: (x-Close1)/Close1)
FacebookDf["sum_pct_change"].tail()
Date
2017-12-22    0.516344
2017-12-26    0.505990
2017-12-27    0.519938
2017-12-28    0.522506
2017-12-29    0.510012
Name: sum_pct_change, dtype: float64

数据可视化

import matplotlib.pyplot as plt
# 查看成交量与股价之间的关系
fig=plt.figure(figsize=(10,5))
AliDf.plot(x="Volume",y="Close",kind="scatter")
plt.xlabel("成交量")
plt.ylabel("股价")
plt.title("成交量与股价之间的关系")
plt.show()
# 查看各个参数之间的相关性,与股价与成交量之间呈中度相关
AliDf.corr()
OpenHighLowCloseAdj CloseVolumesum_pct_change
Open1.0000000.9992810.9987980.9982260.9982260.4246860.998226
High0.9992811.0000000.9987820.9990770.9990770.4324670.999077
Low0.9987980.9987821.0000000.9992490.9992490.4014560.999249
Close0.9982260.9990770.9992491.0000001.0000000.4158011.000000
Adj Close0.9982260.9990770.9992491.0000001.0000000.4158011.000000
Volume0.4246860.4324670.4014560.4158010.4158011.0000000.415801
sum_pct_change0.9982260.9990770.9992491.0000001.0000000.4158011.000000

查看各个公司的股价平均值

AliDf["Close"].mean()
141.79179260159364
"""数据准备"""
# 计算每家公司的收盘价平均值
Close_mean={"Alibaba":AliDf["Close"].mean(),
            "Google":GoogleDf["Close"].mean(),
            "Apple":AppleDf["Close"].mean(),
            "Tencent":TencentDf["Close"].mean(),
            "Amazon":AmazonDf["Close"].mean(),
            "Facebook":FacebookDf["Close"].mean()}
CloseMeanSer=pd.Series(Close_mean)
CloseMeanSer.sort_values(ascending=False,inplace=True) 
"""绘制柱状图"""
# 创建画板
fig=plt.figure(figsize=(10,5))
# 绘图
CloseMeanSer.plot(kind="bar")
# 设置x、y轴标签及标题
plt.xlabel("公司")
plt.ylabel("股价平均值(美元)")
plt.title("2017年各公司股价平均值")
# 设置y周标签刻度
plt.yticks(np.arange(0,1100,100))
# 显示y轴网格
plt.grid(True,axis="y")
# 显示图像
plt.show()

亚马逊和谷歌的平均股价很高,远远超过其他4家,但是仅看平均值并不能代表什么,下面从分布和走势方面查看

查看各公司股价分布情况

"""数据准备"""
# 将6家公司的收盘价整合到一起
CloseCollectDf=pd.concat([AliDf["Close"],
                          GoogleDf["Close"],
                          AppleDf["Close"],
                          TencentDf["Close"],
                          AmazonDf["Close"],
                          FacebookDf["Close"]],axis=1)
CloseCollectDf.columns=["Alibaba","Google","Apple","Tencent","Amazon","Facebook"]
"""绘制箱型图"""
# 创建画板
fig=plt.figure(figsize=(20,10))
fig.suptitle("2017年各公司股价分布",fontsize=18)
# 子图1
ax1=plt.subplot(121)
CloseCollectDf.plot(ax=ax1,kind="box")
plt.xlabel("公司")
plt.ylabel("股价(美元)")
plt.title("2017年各公司股价分布")
plt.grid(True,axis="y")
# 因谷歌和亚马逊和两外四家的差别较大,分开查看,
# 子图2
ax2=plt.subplot(222)
CloseCollectDf[["Google","Amazon"]].plot(ax=ax2,kind="box")
# 设置x、y轴标签及标题
plt.ylabel("股价(美元)")
plt.title("2017年谷歌和亚马逊股价分布")
# 设置y周标签刻度
# plt.yticks(np.arange(0,1300,100))
# 显示y轴网格
plt.grid(True,axis="y")
# 子图3
ax3=plt.subplot(224)
CloseCollectDf[["Alibaba","Apple","Tencent","Facebook"]].plot(ax=ax3,kind="box")
# 设置x、y轴标签及标题
plt.xlabel("公司")
plt.ylabel("股价(美元)")
plt.title("2017年阿里、苹果、腾讯、Facebook股价分布")
# 设置y周标签刻度
# plt.yticks(np.arange(0,1300,100))
# 显示y轴网格
plt.grid(True,axis="y")
plt.subplot
# 显示图像
plt.show()

从箱型图看,谷歌和亚马逊的股价分布较广,且中位数偏上,腾讯股价最为集中,波动最小,相对稳定。

股价走势对比

# 创建画板并设置大小,constrained_layout=True设置自动调整子图之间间距
fig=plt.figure(figsize=(15,10),constrained_layout=True)
# ax=plt.subplots(2,1,sharex=True)
fig.suptitle("股价走势对比",fontsize=18)
"""绘制图像1 """
ax1=plt.subplot(211)
plt.plot(AliDf.index,AliDf["Close"],label="Alibaba")
plt.plot(GoogleDf.index,GoogleDf["Close"],label="Google")
plt.plot(AppleDf.index,AppleDf["Close"],label="Apple")
plt.plot(TencentDf.index,TencentDf["Close"],label="Tencent")
plt.plot(AmazonDf.index,AmazonDf["Close"],label="Amazon")
plt.plot(FacebookDf.index,FacebookDf["Close"],label="Facebook")
# # 设置xy轴标签
plt.xlabel("时间")
plt.ylabel("股价")
# 设置标题
# plt.title("股价走势对比")
# 图例显示位置、大小
plt.legend(loc="upper left",fontsize=12)
# 设置x,y轴间隔,设置旋转角度,以免重叠
plt.xticks(AliDf.index[::10],rotation=45)
plt.yticks(np.arange(0, 1300, step=100))
# 显示网格
plt.grid(True)
"""绘制图像2"""
ax2=plt.subplot(212)
plt.plot(AliDf.index,AliDf["sum_pct_change"],label="Alibaba")
plt.plot(GoogleDf.index,GoogleDf["sum_pct_change"],label="Google")
plt.plot(AppleDf.index,AppleDf["sum_pct_change"],label="Apple")
plt.plot(TencentDf.index,TencentDf["sum_pct_change"],label="Tencent")
plt.plot(AmazonDf.index,AmazonDf["sum_pct_change"],label="Amazon")
plt.plot(FacebookDf.index,FacebookDf["sum_pct_change"],label="Facebook")
# 设置xy轴标签
plt.xlabel("时间")
plt.ylabel("累计增长率")
# 设置标题
# plt.title("股价走势对比")
# 图例显示位置、大小
plt.legend(loc="upper left",fontsize=12)
# 设置x,y轴间隔,设置旋转角度,以免重叠
plt.xticks(AliDf.index[::10],rotation=45)
plt.yticks(np.arange(0, 1.2, step=0.1))
# 显示网格
plt.grid(True)
# 调整子图间距,subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None, hspace=None)
# 显示图像
plt.show()

可以看出,在2017年间,亚马逊和谷歌的股价虽然偏高,涨幅却不如阿里巴巴和腾讯。

总结

观察以上图形,可以得出一下结果:

1、2017年谷歌和亚马逊股价偏高,波动较大,但其涨幅并不高;

2、2017年阿里巴巴和腾讯的股价平均值相对较小,股价波动比较小,其涨幅却很高,分别达到了94.62%和114.36%。

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

关键词: 阿里巴巴 公司股价 数据准备 显示图像 显示位置

X 关闭

X 关闭