首页 > 编程语言 > 详细

Python高级数据处理与可视化(三)

时间:2017-01-24 17:44:20      阅读:713      评论:0      收藏:0      [点我收藏+]

3. Matplotlib图像属性控制

  3.1 色彩和样式(通过help(plt.plot)来查看符号对应含义)

    plt.plot(listKOindex, listKO, ‘g--‘)  # 绿色虚线

    plt.plot(listKOindex, listKO, ‘rD‘)  # 红色钻石

  3.2 文字:为图、横轴和纵轴加标题

    plt.title(‘Stock Statistics of Coca-Cola‘)  # 图标题

    plt.xlabel(‘Month‘)  # 横轴标题

    plt.ylabel(‘Average Close Price‘)  # 纵轴标题

  3.3 其他属性(figure(figsize=(8,6), dpi=50))(pl.legend(loc=‘upper left‘))

技术分享
# -*- 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)  # 图例放在左上角
pl.figure(figsize=(8,6), dpi=100)

    技术分享

  3.4 子图 subplot:将KO公司和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 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()
p1 = pl.subplot(211)

    技术分享

  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()
p1 = pl.axes([.1,.1,0.8,0.8]) # axes([left,bottom,width,height])

    技术分享

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)
closeMeansKO.plot()

    技术分享

  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()
quotesdf.close.plot()

     技术分享

 

Python高级数据处理与可视化(三)

原文:http://www.cnblogs.com/wnzhong/p/6347489.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!