In [1]:
# 啟動互動式繪圖環境
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import numpy as np
import pandas as pd
from numpy import random
import matplotlib.pyplot as plt
with open ('/Users/wy/Desktop/nc0050.txt','r') as f:
    line = f.readlines()
line[0].rstrip()
ll=[]
for a in line[1:]:
    l =[]
    a = a.rstrip()
    a = a.split(',')
    l.append(a[0])
    for a in a[1:]:
        l.append(float(a))
    ll.append(l)
rowname = ['Date','Open','High','Low','Close','Volume','Adj Close']
stock_df = pd.DataFrame(ll, columns=rowname)

In [3]:
stock_df.head()


Out[3]:
Date Open High Low Close Volume Adj Close
0 2014-12-08 67.05 67.05 66.35 66.35 7633033 66.35
1 2014-12-05 67.10 67.10 66.75 66.85 4318019 66.80
2 2014-12-04 67.25 67.45 67.10 67.10 11366115 67.10
3 2014-12-03 65.80 67.00 65.70 66.85 11865879 66.80
4 2014-12-02 66.35 66.50 65.65 65.80 19739586 65.75

In [4]:
# 顯示該欄的細部資訊 count,mean,std... 注意須為float or int
stock_df.Open.describe()


Out[4]:
count    230.000000
mean      63.498043
std        3.712743
min       55.950000
25%       60.325000
50%       64.225000
75%       66.650000
max       69.700000
dtype: float64

In [5]:
fig = plt.figure()
# (2,2,1) 2 × 2 , 第幾個
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)



In [6]:
fig = plt.figure();
ax = fig.add_subplot(1, 1, 1)
# 在同一張圖上畫出兩組scatter,color可用色碼
ax.scatter(np.arange(1,30), np.arange(1,30),color='#9D442F')
ax.scatter(np.arange(40,50), np.arange(40,50),color='#5D947E')


Out[6]:
<matplotlib.collections.PathCollection at 0x107b1a590>

In [7]:
fig = plt.figure(); 
ax = fig.add_subplot(1, 1, 1)
# cumsum()累計,走的距離
ax.plot(random.randn(1000).cumsum())

fig = plt.figure(); 
ax = fig.add_subplot(1, 1, 1)
ax.plot(random.randn(1000).cumsum())
# 重新設定 x軸區間
ax.set_xticks([0, 250, 500, 750, 1000])

fig = plt.figure(); 
ax = fig.add_subplot(1, 1, 1)
ax.plot(random.randn(1000).cumsum())
# 重新設定 x軸區間 可用文字代替 rotation:旋轉角度 fontsize:字體大小
labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five','six'],rotation=30, fontsize='large')
# 圖的命名
ax.set_title('My first matplotlib plot')
# x軸的命名
ax.set_xlabel('Stages')
# y軸的命名
ax.set_ylabel('distance')


Out[7]:
<matplotlib.text.Text at 0x107dfda10>

In [8]:
fig = plt.figure(); ax = fig.add_subplot(1, 1, 1)
ax.plot(random.randn(1000).cumsum(), 'k', label='one')
ax.plot(random.randn(1000).cumsum(), 'k--', label='two')
ax.plot(random.randn(1000).cumsum(), 'k.', label='three')
# legend 圖例,loc='best'此行為控制 legend的位置 若不用best 則可能會蓋住圖
ax.legend(loc='best')


Out[8]:
<matplotlib.legend.Legend at 0x107dba7d0>

In [9]:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# 抽取 df 的 Open欄位
ax.plot(stock_df['Open'])
# 在(50,60)加上文字
ax.text(50, 60, 'Hello world!', family='monospace', fontsize=10)
crisis_data = [(50,'11'),(100,'12'),(150,'13')]



In [10]:
# save as svg
fig.savefig('/Users/wy/Desktop/figpath.svg')
# save as png dpi:解析度 bbox_inches='tight' 最小圖範圍
# The explicit file format to use ('png', 'pdf', 'svg', 'ps', 'eps', ...)
fig.savefig('/Users/wy/Desktop/figpath.png', dpi=400, bbox_inches='tight')

In [11]:
# Line Plots

s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
s.plot()
df = pd.DataFrame(np.random.randn(10, 4),columns=['A', 'B', 'C', 'D'],index=np.arange(0, 100, 10))
df.plot()


Out[11]:
<matplotlib.axes.AxesSubplot at 0x109c25910>

In [12]:
fig, axes = plt.subplots(2, 1)
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
# alpha透明度 越小越透明
data.plot(kind='bar', ax=axes[0], color='#9D442F', alpha=0.7)
data.plot(kind='barh', ax=axes[1], color='#5D947E', alpha=0.5)


Out[12]:
<matplotlib.axes.AxesSubplot at 0x109d10e10>

In [13]:
stock_df.head()


Out[13]:
Date Open High Low Close Volume Adj Close
0 2014-12-08 67.05 67.05 66.35 66.35 7633033 66.35
1 2014-12-05 67.10 67.10 66.75 66.85 4318019 66.80
2 2014-12-04 67.25 67.45 67.10 67.10 11366115 67.10
3 2014-12-03 65.80 67.00 65.70 66.85 11865879 66.80
4 2014-12-02 66.35 66.50 65.65 65.80 19739586 65.75

In [14]:
df = pd.DataFrame(np.random.rand(6, 4),
index=['one', 'two', 'three', 'four', 'five', 'six'],columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
# stacked=True 把同x軸的y值堆疊
df.plot(kind='bar')
df.plot(kind='bar', stacked=True)
# Normalize to sum to 1
df = df.div(df.sum(1).astype(float), axis=0)
df.plot(kind='bar', stacked=True)


Out[14]:
<matplotlib.axes.AxesSubplot at 0x10a495850>