Python基础教程实践2,画幅好画(win7,64位系统)

来源:互联网 发布:mac无法删除windows 编辑:程序博客网 时间:2024/04/30 15:40

一、下载reportlab

  • 下载地址http://www.reportlab.com/ftp/
  • 下载reportlab-2.5.win32-py2.7.exe

二、获取数据源


由于宿舍没有网络,只能能先下载好txt,然后放到虚拟机linux,里面的apache服务器里面,以此来实现。下载地址http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt

三、代码


Sunspots_roto.py

[python] view plaincopy
  1. from urllib import urlopen  
  2. from reportlab.graphics.shapes import *  
  3. from reportlab.graphics.charts.lineplots import LinePlot  
  4. from reportlab.graphics.charts.textlabels import Label  
  5. from reportlab.graphics import renderPDF  
  6.   
  7.   
  8. URL='http://192.168.177.128/Predict.txt'  
  9. COMMENT_CHAR ='#:'  
  10.   
  11.   
  12. drawing = Drawing(400,200)  
  13. data = []  
  14. for line in urlopen(URL).readlines():  
  15.     if not line.isspace() and not line[0in COMMENT_CHAR:  
  16.         data.append([float(n) for n in line.split()])  
  17.           
  18. pred = [row[2for row in data]  
  19. high = [row[3for row in data]  
  20. low = [row[4for row in data]  
  21. times = [row[0] + row[1]/12.0 for row in data]  
  22.   
  23.   
  24. lp = LinePlot()  
  25. lp.x = 50  
  26. lp.y = 50  
  27. lp.height =125  
  28. lp.width =300  
  29. lp.data = [zip(times,pred),zip(times,high),zip(times,low)]  
  30. lp.lines[0].strokeColor = colors.blue  
  31. lp.lines[1].strokeColor = colors.red  
  32. lp.lines[2].strokeColor = colors.green  
  33.   
  34.   
  35. drawing.add(lp)  
  36.   
  37.   
  38. drawing.add(String(250,150,'Sunspots',  
  39.                    fontSize=14,fillColor=colors.red))  
  40.   
  41.   
  42. renderPDF.drawToFile(drawing,'report2.pdf','Sunspots')  

三、实现效果


0 0