In [6]:
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
from bokeh.charts import Scatter, output_notebook, show
from highcharts import Highchart
from ipywidgets import interact
from IPython.display import HTML,YouTubeVideo
%matplotlib inline

In [7]:
ticker = ['AAPL','MSFT','GOOG']
start = '2017-01-01'#datetime(2016,1,1)
end = '2017-04-30'#datetime(2016,12,31)
df = web.DataReader(ticker,'google',start,end)['Close',:,:]
df


Out[7]:
AAPL GOOG MSFT
Date
2017-01-03 116.15 786.14 62.58
2017-01-04 116.02 786.90 62.30
2017-01-05 116.61 794.02 62.30
2017-01-06 117.91 806.15 62.84
2017-01-09 118.99 806.65 62.64
2017-01-10 119.11 804.79 62.62
2017-01-11 119.75 807.91 63.19
2017-01-12 119.25 806.36 62.61
2017-01-13 119.04 807.88 62.70
2017-01-17 120.00 804.61 62.53
2017-01-18 119.99 806.07 62.50
2017-01-19 119.78 802.18 62.30
2017-01-20 120.00 805.02 62.74
2017-01-23 120.08 819.31 62.96
2017-01-24 119.97 823.87 63.52
2017-01-25 121.88 835.67 63.68
2017-01-26 121.94 832.15 64.27
2017-01-27 121.95 823.31 65.78
2017-01-30 121.63 802.32 65.13
2017-01-31 121.35 796.79 64.65
2017-02-01 128.75 795.70 63.58
2017-02-02 128.53 798.53 63.17
2017-02-03 129.08 801.49 63.68
2017-02-06 130.29 801.34 63.64
2017-02-07 131.53 806.97 63.43
2017-02-08 132.04 808.38 63.34
2017-02-09 132.42 809.56 64.06
2017-02-10 132.12 813.67 64.00
2017-02-13 133.29 819.24 64.72
2017-02-14 135.02 820.45 64.57
... ... ... ...
2017-03-17 139.99 852.12 64.87
2017-03-20 141.46 848.40 64.93
2017-03-21 139.84 830.46 64.21
2017-03-22 141.42 829.59 65.03
2017-03-23 140.92 817.58 64.87
2017-03-24 140.64 814.43 64.98
2017-03-27 140.88 819.51 65.10
2017-03-28 143.80 820.92 65.29
2017-03-29 144.12 831.41 65.47
2017-03-30 143.93 831.50 65.71
2017-03-31 143.66 829.56 65.86
2017-04-03 143.70 838.55 65.55
2017-04-04 144.77 834.57 65.73
2017-04-05 144.02 831.41 65.56
2017-04-06 143.66 827.88 65.73
2017-04-07 143.34 824.67 65.68
2017-04-10 143.17 824.73 65.53
2017-04-11 141.63 823.35 65.48
2017-04-12 141.80 824.32 65.23
2017-04-13 141.05 823.56 64.95
2017-04-17 141.83 837.17 65.48
2017-04-18 141.20 836.82 65.39
2017-04-19 140.68 838.21 65.04
2017-04-20 142.44 841.65 65.50
2017-04-21 142.27 843.19 66.40
2017-04-24 143.64 862.76 67.53
2017-04-25 144.53 872.30 67.92
2017-04-26 143.68 871.73 67.83
2017-04-27 143.79 874.25 68.27
2017-04-28 143.65 905.96 68.46

81 rows × 3 columns

Youtubeの動画を埋め込めます。


In [ ]:
YouTubeVideo('pYXOF0jziGM')

pandasで簡単にプロットできます。


In [4]:
df[['AAPL','MSFT']].plot()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-4e1d197975e5> in <module>()
----> 1 df[['AAPL','MSFT']].plot().show()

AttributeError: 'AxesSubplot' object has no attribute 'show'

3次元プロット


In [ ]:
fig3d = plt.figure()
ax3d = fig3d.add_subplot(111,projection='3d')
ax3d.scatter3D(df['AAPL'],df['MSFT'],df['GOOG'])

seabornできれいなプロット


In [ ]:
s = df['AAPL']
sns.set_style('dark')
sns.distplot(s)

In [ ]:
sns.jointplot(x='AAPL',y='GOOG',data=df[['AAPL','GOOG']])

bokeh


In [ ]:
output_notebook()
p = Scatter(data=df,x='AAPL',y='GOOG')#,color='species',legend='top_left')
show(p)

highcharts


In [8]:
options = {
    'chart': {
        'type': 'scatter',
    },
}
H = Highchart()
H.set_dict_options(options)
data = list(zip(df['AAPL'], df['GOOG']))
H.add_data_set(data, 'scatter')
H


Out[8]:

In [9]:
hc=H.buildhtml()

In [10]:
HTML(hc)


Out[10]:
Loading....

In [11]:
%pwd


Out[11]:
'C:\\Users\\Ryo\\workspace\\study_python-master'

In [12]:
open('test.html', 'w').write(hc)


Out[12]:
2954

In [13]:
import webbrowser

In [14]:
webbrowser.open('test.html')


Out[14]:
True

In [ ]:
@interact
def plot3d(v1=(0,90,1),v2=(0,90,1)):
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.scatter3D(df['AAPL'],df['GOOG'],df['MSFT'])
    ax.set_xlabel("AAPL")
    ax.set_ylabel("GOOG")
    ax.set_zlabel("MSFT")
    ax.view_init(v1,v2)#見る方向を変える
    plt.show()

In [ ]: