urllib获取太阳黑子图表程序

 1 # -*- coding:utf-8 -*-
 2 # Author:Vincent Zhang
 3 
 4 from urllib.request import urlopen
 5 from reportlab.graphics.shapes import *
 6 from reportlab.graphics.charts.lineplots import LinePlot
 7 from reportlab.graphics.charts.textlabels import Label
 8 from reportlab.graphics import renderPDF
 9 
10 URL = 'ftp://ftp.swpc.noaa.gov/pub/weekly/Predict.txt'
11 COMMENT_CHARS = '#:'
12 drawing = Drawing(400, 200)
13 data = []
14 for line in urlopen(URL).readlines():
15     line = line.decode()
16     if not line.isspace() and line[0] not in COMMENT_CHARS:
17         data.append([float(n) for n in line.split()])
18 pred = [row[2] for row in data]
19 high = [row[3] for row in data]
20 low = [row[4] for row in data]
21 times = [row[0] + row[1] / 12.0 for row in data]
22 lp = LinePlot()
23 lp.x = 50
24 lp.y = 50
25 lp.height = 125
26 lp.width = 300
27 lp.data = [list(zip(times, pred)),
28            list(zip(times, high)),
29            list(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 drawing.add(lp)
34 drawing.add(String(250, 150, 'Sunspots',
35                    fontSize=14, fillColor=colors.red))
36 renderPDF.drawToFile(drawing, 'report2.pdf', 'Sunspots')
View Code
原文地址:https://www.cnblogs.com/zijue/p/10164027.html