3. Matplotlib圖像屬性控制html
3.1 色彩和樣式(經過help(plt.plot)來查看符號對應含義)app
plt.plot(listKOindex, listKO, 'g--') # 綠色虛線ide
plt.plot(listKOindex, listKO, 'rD') # 紅色鑽石spa
3.2 文字:爲圖、橫軸和縱軸加標題3d
plt.title('Stock Statistics of Coca-Cola') # 圖標題code
plt.xlabel('Month') # 橫軸標題htm
plt.ylabel('Average Close Price') # 縱軸標題blog
3.3 其餘屬性(figure(figsize=(8,6), dpi=50))(pl.legend(loc='upper left'))utf-8
# -*- coding: utf-8 -*- """ Created on Tue Jan 24 13:07:01 2017 @author: Wayne """ import pylab as pl import numpy as np pl.figure(figsize=(8,6), dpi=100) # 建立一個8*6 點(point)的圖,並設置分辨率爲100 t = np.arange(0.,4.,0.1) pl.plot(t,t,color='red',linestyle='-',linewidth=3,label='Line 1') pl.plot(t,t+2,color='green',linestyle='',marker='*',linewidth=3,label='Line2') pl.plot(t,t**2,color='blue',linestyle='',marker='+',linewidth=3,label='Line3') pl.legend(loc='upper left') # 圖例放在左上角
3.4 子圖 subplot:將KO公司和IBM公司近一年來股票收盤價的月平均價繪製在一張圖中get
# -*- coding: utf-8 -*- """ Created on Tue Jan 24 13:07:01 2017 @author: Wayne """ from matplotlib.finance import quotes_historical_yahoo_ochl from datetime import date, datetime import time import pandas as pd import matplotlib.pylab as pl today = date.today() start = (today.year-1, today.month, today.day) quotes1 = quotes_historical_yahoo_ochl('KO', start, today) quotes2 = quotes_historical_yahoo_ochl('IBM', start, today) fields = ['date','open','close','high','low','volume'] list1 = [] list2 = [] for i in range(0,len(quotes1)): x = date.fromordinal(int(quotes1[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list1.append(y) for i in range(0,len(quotes2)): x = date.fromordinal(int(quotes2[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list2.append(y) quoteskodf1 = pd.DataFrame(quotes1, index = list1, columns = fields) quoteskodf1 = quoteskodf1.drop(['date'], axis = 1) quoteskodf2 = pd.DataFrame(quotes2, index = list2, columns = fields) quoteskodf2 = quoteskodf2.drop(['date'], axis = 1) listtemp1 = [] listtemp2 = [] for i in range(0,len(quoteskodf1)): temp = time.strptime(quoteskodf1.index[i],"%Y-%m-%d") # 提取月份 listtemp1.append(temp.tm_mon) for i in range(0,len(quoteskodf2)): temp = time.strptime(quoteskodf2.index[i],"%Y-%m-%d") # 提取月份 listtemp2.append(temp.tm_mon) tempdf1 = quoteskodf1.copy() tempdf1['month'] = listtemp1 closeMeans1 = tempdf1.groupby('month').mean().close list11 = [] for i in range(1,13): list11.append(closeMeans1[i]) list1Index = closeMeans1.index tempdf2 = quoteskodf2.copy() tempdf2['month'] = listtemp2 closeMeans2 = tempdf2.groupby('month').mean().close list22 = [] for i in range(1,13): list22.append(closeMeans2[i]) list2Index = closeMeans2.index p1 = pl.subplot(211) p2 = pl.subplot(212) p1.set_title('Stock Average Close : KO vs IBM') p1.plot(list1Index,list11,color='r',marker='o',label='KO') p1.legend(loc='upper left') p2.plot(list2Index,list22,color='g',marker='o',label='IBM') p2.set_xlabel('month') p2.legend(loc='upper left') pl.show()
3.5 子圖 axes:p1 = pl.axes([.1,.1,0.8,0.8]) # axes([left,bottom,width,height])(參數範圍爲(0,1))
參考:http://www.xuebuyuan.com/491377.html
p1 = pl.axes([.1,.1,0.8,0.8]) # axes([left,bottom,width,height]) p2 = pl.axes([.3,.15,0.4,0.4]) p1.plot(list1Index,list11,color='r',marker='o',label='KO') p1.legend(loc='upper left') p2.plot(list2Index,list22,color='g',marker='o',label='IBM') p2.legend(loc='upper left') pl.show()
4. Pandas做圖
4.1 直接對Series進行繪圖:closeMeansKO.plot() # KO公司12個月的月平均收盤價折線圖
from matplotlib.finance import quotes_historical_yahoo_ochl from datetime import date, datetime import time import pandas as pd import matplotlib.pyplot as plt today = date.today() start = (today.year-1, today.month, today.day) quotes = quotes_historical_yahoo_ochl('KO', start, today) fields = ['date','open','close','high','low','volume'] list1 = [] for i in range(0,len(quotes)): x = date.fromordinal(int(quotes[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list1.append(y) #print(list1) quoteskodf = pd.DataFrame(quotes, index = list1, columns = fields) quoteskodf = quoteskodf.drop(['date'], axis = 1) #print(quotesdf) listtemp = [] for i in range(0,len(quoteskodf)): temp = time.strptime(quoteskodf.index[i],"%Y-%m-%d") # 提取月份 listtemp.append(temp.tm_mon) #print(listtemp) # 「print listtemp」 in Python 2.x tempkodf = quoteskodf.copy() tempkodf['month'] = listtemp closeMeansKO = tempkodf.groupby('month').mean().close closeMeansKO.plot() plt.title('Stock Statistics of Coca-cola') plt.xlabel('month') plt.ylabel('average close price')
4.2 直接對DataFrame進行繪圖:quotesdf.close.plot() # IBM公司近一年收盤價折線圖
# -*- coding: utf-8 -*- """ Created on Tue Jan 24 13:07:01 2017 @author: Wayne """ from matplotlib.finance import quotes_historical_yahoo_ochl from datetime import date, datetime import pandas as pd import matplotlib.pyplot as plt today = date.today() start = (today.year-1, today.month, today.day) quotes = quotes_historical_yahoo_ochl('IBM', start, today) fields = ['date','open','close','high','low','volume'] list1 = [] for i in range(0,len(quotes)): x = date.fromordinal(int(quotes[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list1.append(y) #print(list1) quotesdf = pd.DataFrame(quotes, index = list1, columns = fields) quotesdf = quotesdf.drop(['date'], axis = 1) plt.title('IBM Stock Close Price') plt.xlabel('date') plt.ylabel('close price') quotesdf.close.plot()
4.3 Pandas控制圖像形式:quotesdf.plot(kind='bar')(用柱狀圖比較Intel和GE兩家科技公司2014年10月的股票收盤價)
# -*- coding: utf-8 -*- """ Created on Tue Jan 24 13:07:01 2017 @author: Wayne """ from matplotlib.finance import quotes_historical_yahoo_ochl from datetime import date, datetime import pandas as pd start = date(2016,10,01) end = date(2016,10,16) quotes1 = quotes_historical_yahoo_ochl('INTC', start, end) quotes2 = quotes_historical_yahoo_ochl('GE', start, end) fields = ['date','open','close','high','low','volume'] list1 = [] list2 = [] for i in range(0,len(quotes1)): x = date.fromordinal(int(quotes1[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list1.append(y) for i in range(0,len(quotes2)): x = date.fromordinal(int(quotes2[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list2.append(y) quotesdf1 = pd.DataFrame(quotes1, index = list1, columns = fields) quotesdf1 = quotesdf1.drop(['date'], axis = 1) quotesdf2 = pd.DataFrame(quotes2, index = list2, columns = fields) quotesdf2 = quotesdf2.drop(['date'], axis = 1) quotesdf = pd.DataFrame() quotesdf['INTCclose'] = quotesdf1.close quotesdf['GEclose'] = quotesdf2.close quotesdf.plot(kind='bar')
[ quotesdf.plot(kind='bar') ] [ quotesdf.plot(kind='barh') ] [ quotesdf.plot(kind='scatter', x='INTCclose', y='GEclose', color='g') ]
4.4 Pandas控制圖像屬性:將KO公司和IBM公司近12個月收盤平均價繪製在同一張圖上
closeMeans1.plot(color='r',marker='D',label='Coco-Cola') closeMeans2.plot(color='g',marker='D',label='IBM') plt.legend(loc='center right')
4.5 Pandas控制圖像屬性:繪圖顯示Intel和GE兩家科技公司2014年10月的股票收盤價的機率分佈
4.6 練習:用折線圖比較Microsoft 和Intel 在2015 年每月股票的最高收盤價。圖標題爲「MaxClose of MS and INTEL」,橫座標是時間,縱座標是價格
# -*- coding: utf-8 -*- """ Created on Tue Jan 24 13:07:01 2017 @author: Wayne """ from matplotlib.finance import quotes_historical_yahoo_ochl from datetime import date, datetime import time import pandas as pd import matplotlib.pylab as pl start = date(2015,01,01) end = date(2016,01,01) quotes1 = quotes_historical_yahoo_ochl('MSFT', start, end) quotes2 = quotes_historical_yahoo_ochl('INTC', start, end) fields = ['date','open','close','high','low','volume'] list1 = [] list2 = [] for i in range(0,len(quotes1)): x = date.fromordinal(int(quotes1[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list1.append(y) for i in range(0,len(quotes2)): x = date.fromordinal(int(quotes2[i][0])) y = datetime.strftime(x,'%Y-%m-%d') list2.append(y) quoteskodf1 = pd.DataFrame(quotes1, index = list1, columns = fields) quoteskodf1 = quoteskodf1.drop(['date'], axis = 1) quoteskodf2 = pd.DataFrame(quotes2, index = list2, columns = fields) quoteskodf2 = quoteskodf2.drop(['date'], axis = 1) listtemp1 = [] listtemp2 = [] for i in range(0,len(quoteskodf1)): temp = time.strptime(quoteskodf1.index[i],"%Y-%m-%d") # 提取月份 listtemp1.append(temp.tm_mon) for i in range(0,len(quoteskodf2)): temp = time.strptime(quoteskodf2.index[i],"%Y-%m-%d") # 提取月份 listtemp2.append(temp.tm_mon) tempdf1 = quoteskodf1.copy() tempdf1['month'] = listtemp1 closeMeans1 = tempdf1.groupby('month').max().close list11 = [] for i in range(1,13): list11.append(closeMeans1[i]) list1Index = closeMeans1.index tempdf2 = quoteskodf2.copy() tempdf2['month'] = listtemp2 closeMeans2 = tempdf2.groupby('month').max().close list22 = [] for i in range(1,13): list22.append(closeMeans2[i]) list2Index = closeMeans2.index p1 = pl.subplot(211) p2 = pl.subplot(212) p1.set_title('Max close price of MS and INTEL') p1.plot(list1Index,list11,color='r',marker='o',label='MS') p1.set_ylabel('price') p1.legend(loc='upper left') p2.plot(list2Index,list22,color='g',marker='o',label='INTEL') p2.set_xlabel('month') p2.set_ylabel('price') p2.legend(loc='lower left') pl.show()