In [1]:
# 啟動互動式繪圖環境
%pylab inline
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]:
In [4]:
# 顯示該欄的細部資訊 count,mean,std... 注意須為float or int
stock_df.Open.describe()
Out[4]:
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]:
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]:
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]:
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]:
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]:
In [13]:
stock_df.head()
Out[13]:
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]: