listing21-3.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from urllib.request 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. URL = 'ftp://ftp.swpc.noaa.gov/pub/weekly/Predict.txt'
  7. COMMENT_CHARS = '#:'
  8. drawing = Drawing(400, 200)
  9. data = []
  10. for line in urlopen(URL).readlines():
  11. line = line.decode()
  12. if not line.isspace() and line[0] not in COMMENT_CHARS:
  13. data.append([float(n) for n in line.split()])
  14. pred = [row[2] for row in data]
  15. high = [row[3] for row in data]
  16. low = [row[4] for row in data]
  17. times = [row[0] + row[1]/12.0 for row in data]
  18. lp = LinePlot()
  19. lp.x = 50
  20. lp.y = 50
  21. lp.height = 125
  22. lp.width = 300
  23. lp.data = [list(zip(times, pred)),
  24. list(zip(times, high)),
  25. list(zip(times, low))]
  26. lp.lines[0].strokeColor = colors.blue
  27. lp.lines[1].strokeColor = colors.red
  28. lp.lines[2].strokeColor = colors.green
  29. drawing.add(lp)
  30. drawing.add(String(250, 150, 'Sunspots',
  31. fontSize=14, fillColor=colors.red))
  32. renderPDF.drawToFile(drawing, 'report2.pdf', 'Sunspots')