python项目画幅好画_python基础教程总结15——2 画幅好画
標(biāo)簽:
要求:從Internet上下載數(shù)據(jù)文件; ?分析數(shù)據(jù)文件并提取感興趣的部分
工具:圖形生成包(ReportLab,PYX等)
數(shù)據(jù):太陽(yáng)黑子和射電輻射流量(http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt)
1. 簡(jiǎn)單實(shí)現(xiàn)
1.1 用ReportLab畫(huà)圖
將字符串“Hello world”畫(huà)在一個(gè)100*100像素的大小的PDF圖形中間:創(chuàng)建一個(gè)給定大小的圖紙(drawing);然后創(chuàng)建帶有某些屬性的圖形元素,本例是一個(gè)string對(duì)象;接著將元素添加到圖紙中去;最后將圖紙生成為PDF格式并保存。
fromreportlab.graphics.shapes import Drawing ,Stringfromreportlab.graphics import renderPDF
d=Drawing(100,100)
s=String(50,50,‘Hello,world!‘,textAnchor=‘middle‘)
d.add(s)
renderPDF.drawToFile(d,‘hello.pdf‘,‘A simple PDF file‘)
1.2 原型
from reportlab.lib importcolorsfrom reportlab.graphics.shapes import*
from reportlab.graphics importrenderPDF
data=[#Year Month Predicted High Low
(2007, 8, 113.2, 114.2, 112.2),
(2007, 9, 112.8, 115.8, 109.8),
(2007, 10, 111.0, 116.0, 106.0),
(2007, 11, 109.8, 116.8, 102.8),
(2007, 12, 107.3, 115.3, 99.3),
(2008, 1, 105.2, 114.2, 96.2),
(2008, 2, 104.1, 114.1, 94.1),
(2008, 3, 99.9, 110.9, 88.9),
(2008, 4, 94.8, 106.8, 82.8),
(2008, 5, 91.2, 104.2, 78.2)
]
drawing=Drawing(200,150)
pred=[row[2]-40 for row indata]
high=[row[3]-40 for row indata]
low=[row[4]-40 for row indata]
times=[200*((row[0]+row[1]/12.0)-2007)-110 for row indata]#PolyLine畫(huà)折線
drawing.add(PolyLine(zip(times,pred),strokeColor=colors.blue))
drawing.add(PolyLine(zip(times,high),strokeColor=colors.red))
drawing.add(PolyLine(zip(times,low),strokeColor=colors.green))
drawing.add(String(65,115,‘Sunspots‘,fontSize=18,fillColor=colors.red))
renderPDF.drawToFile(drawing,‘report1.pdf‘,‘Sunsports‘)
2. 再次實(shí)現(xiàn)
1)獲取數(shù)據(jù)
使用標(biāo)準(zhǔn)模塊urllib可以從Internet獲取文件。模塊中的urlopen函數(shù)類似于open函數(shù),參數(shù)是一個(gè)URL而不是文件名。打開(kāi)閱讀文件時(shí),需要過(guò)濾掉不需要的內(nèi)容。文件包含空行以及每行都以一些特殊的字符開(kāi)始(#和:),應(yīng)該忽略。
#URL保存在變量URL中,變量COMMENT_CHARS設(shè)定為字符串‘#:‘
data=[]for line inurlopen(URL).readlines():if not line.isspace() and not line[0] inCOMMENT_CHARS:
data.append([float(n)for n in line.split() ])
2)使用LinePlot類
LinePlot類的實(shí)例化不需要任何參數(shù),然后在將它添加到Drawing前設(shè)置特性:想,一,height,width以及data(元組列表)
3)最終程序:
from urllib importurlopenfrom reportlab.graphics.shapes import *
from reportlab.graphics.charts.lineplots importLinePlotfrom reportlab.graphics.charts.textlabels importLabelfrom reportlab.graphics importrenderPDF
URL= ‘http://services.swpc.noaa.gov/text/predicted-sunspot-radio-flux.txt‘COMMENT_CHARS= ‘#:‘drawing= Drawing(400, 200)
data=[]for line inurlopen(URL).readlines():if not line.isspace() and not line[0] inCOMMENT_CHARS:
data.append([float(n)for n inline.split()])
pred= [row[2] for row indata]
high= [row[3] for row indata]
low= [row[4] for row indata]
times= [row[0] + row[1]/12.0 for row indata]
lp=LinePlot()
lp.x= 50lp.y= 50lp.height= 125lp.width= 300lp.data=[zip(times, pred),zip(times,high),zip(times, low)]
lp.lines[0].strokeColor=colors.blue
lp.lines[1].strokeColor =colors.red
lp.lines[2].strokeColor =colors.green
drawing.add(lp)
drawing.add(String(250,150, ‘Sunspots‘,fontSize=14,fillColor=colors.red))
renderPDF.drawToFile(drawing,‘report3.pdf‘,‘Sunspots‘)
標(biāo)簽:
總結(jié)
以上是生活随笔為你收集整理的python项目画幅好画_python基础教程总结15——2 画幅好画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 红米android版本,微信红米低版本下
- 下一篇: python将数据写入Excel