# 基本信息
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
# 股票数据读取
import pandas_datareader as pdr
# 可视化
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline
# time
from datetime import datetime
代码语言:javascript复制start = datetime(2015,9,20)
# 获取阿里股票数据
alibaba = pdr.get_data_yahoo('BABA', start=start)
# 获取亚马逊数据
amazon = pdr.get_components_yahoo('AMZN')
代码语言:javascript复制# 数据存储alibaba.to_csv('url')
# 读取本地数据
alibaba = pd.read_csv('/Users/bennyrhys/Desktop/数据分析可视化-数据集/homework/BABA.csv',index_col=0)
amazon = pd.read_csv('/Users/bennyrhys/Desktop/数据分析可视化-数据集/homework/AMZN.csv',index_col=0)
代码语言:javascript复制alibaba.head()
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2015-09-21 | 65.379997 | 66.400002 | 62.959999 | 63.900002 | 63.900002 | 22355100 |
2015-09-22 | 62.939999 | 63.270000 | 61.580002 | 61.900002 | 61.900002 | 14897900 |
2015-09-23 | 61.959999 | 62.299999 | 59.680000 | 60.000000 | 60.000000 | 22684600 |
2015-09-24 | 59.419998 | 60.340000 | 58.209999 | 59.919998 | 59.919998 | 20645700 |
2015-09-25 | 60.630001 | 60.840000 | 58.919998 | 59.240002 | 59.240002 | 17009100 |
amazon.head()
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2015-09-21 | 544.330017 | 549.780029 | 539.590027 | 548.390015 | 548.390015 | 3283300 |
2015-09-22 | 539.710022 | 543.549988 | 532.659973 | 538.400024 | 538.400024 | 3841700 |
2015-09-23 | 538.299988 | 541.210022 | 534.000000 | 536.070007 | 536.070007 | 2237600 |
2015-09-24 | 530.549988 | 534.559998 | 522.869995 | 533.750000 | 533.750000 | 3501000 |
2015-09-25 | 542.570007 | 542.799988 | 521.400024 | 524.250000 | 524.250000 | 4031000 |
# 闭盘走势
alibaba['Adj Close'].plot(legend=True)
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a252e4ad0>
代码语言:javascript复制# 交易量
alibaba['Volume'].plot(legend=True)
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a216b9790>
代码语言:javascript复制# 闭盘走势-两家公司
alibaba['Adj Close'].plot()
amazon['Adj Close'].plot()
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a26641290>
代码语言:javascript复制# 统计每日差值
alibaba['high-low'] = alibaba['High'] - alibaba['Low']
alibaba.head()
Open | High | Low | Close | Adj Close | Volume | high-low | |
---|---|---|---|---|---|---|---|
Date | |||||||
2015-09-21 | 65.379997 | 66.400002 | 62.959999 | 63.900002 | 63.900002 | 22355100 | 3.440003 |
2015-09-22 | 62.939999 | 63.270000 | 61.580002 | 61.900002 | 61.900002 | 14897900 | 1.689998 |
2015-09-23 | 61.959999 | 62.299999 | 59.680000 | 60.000000 | 60.000000 | 22684600 | 2.619999 |
2015-09-24 | 59.419998 | 60.340000 | 58.209999 | 59.919998 | 59.919998 | 20645700 | 2.130001 |
2015-09-25 | 60.630001 | 60.840000 | 58.919998 | 59.240002 | 59.240002 | 17009100 | 1.920002 |
alibaba['high-low'].plot()
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a26668a90>
代码语言:javascript复制# 每天的改变
alibaba['daily-return'] = alibaba['Adj Close'].pct_change()
alibaba.head()
Open | High | Low | Close | Adj Close | Volume | high-low | daily-return | |
---|---|---|---|---|---|---|---|---|
Date | ||||||||
2015-09-21 | 65.379997 | 66.400002 | 62.959999 | 63.900002 | 63.900002 | 22355100 | 3.440003 | NaN |
2015-09-22 | 62.939999 | 63.270000 | 61.580002 | 61.900002 | 61.900002 | 14897900 | 1.689998 | -0.031299 |
2015-09-23 | 61.959999 | 62.299999 | 59.680000 | 60.000000 | 60.000000 | 22684600 | 2.619999 | -0.030695 |
2015-09-24 | 59.419998 | 60.340000 | 58.209999 | 59.919998 | 59.919998 | 20645700 | 2.130001 | -0.001333 |
2015-09-25 | 60.630001 | 60.840000 | 58.919998 | 59.240002 | 59.240002 | 17009100 | 1.920002 | -0.011348 |
alibaba['daily-return'].plot()
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a28c20410>
代码语言:javascript复制# 美化图
alibaba['daily-return'].plot(figsize=(10,4),linestyle='--',marker='o')
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a29240a90>
代码语言:javascript复制# 直方图
alibaba['daily-return'].plot(kind='hist')
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a2786f050>
代码语言:javascript复制sns.distplot(alibaba['daily-return'].dropna(),bins=100, color='purple')
代码语言:javascript复制<matplotlib.axes._subplots.AxesSubplot at 0x1a28bf8710>