第21章 项目2:画幅好画

hello_report.py

1 from reportlab.graphics.shapes import Drawing, String
2 from reportlab.graphics import renderPDF
3 
4 d = Drawing(100.100)
5 s = String(50, 50, 'hello,world!', textAnchor='middle')
6 
7 d.add(s)
8 
9 renderPDF.drawToFile(d, 'hello.pdf', 'A simple PDF file')

sunpots_roto.py

 1 from reportlab.lib import colors
 2 from reportlab.graphics.shapes import *
 3 from reportlab.graphics import renderPDF
 4 
 5 data = [
 6     # Year Month Predicted High Low
 7     (2007, 8, 113.2, 114.2, 112.2),
 8     (2007, 9, 112.8, 115.8, 109.8),
 9     (2007, 10, 110.0, 116.0, 106.0),
10     (2007, 11, 109.8, 116.8, 102.8),
11     (2007, 12, 107.3, 115.3, 99.3),
12     (2008, 1, 105.2, 114.2, 96.2),
13     (2008, 2, 104.1, 114.1, 94.1),
14     (2008, 3, 99.9, 110.9, 88.9),
15     (2008, 4, 94.8, 106.8, 82.8),
16     (2008, 5, 91.2, 104.2, 78.2),
17 ]
18 
19 drawing = Drawing(200, 150)
20 
21 pred = [row[2]-40 for row in data]
22 high = [row[3]-40 for row in data]
23 low =  [row[4]-40 for row in data]
24 times = [200*((row[0] + row[1]/12.0) - 2007)-110 for row in data]
25 
26 drawing.add(PolyLine(tuple(zip(times, pred)), strokeColor=colors.blue))
27 drawing.add(PolyLine(tuple(zip(times, high)), strokeColor=colors.red))
28 drawing.add(PolyLine(tuple(zip(times, low)), strokeColor=colors.green))
29 drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red))
30 
31 renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots')

sunports_master.py

 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 
 7 URL = "ftp://ftp.swpc.noaa.gov/pub/weekly/Predict.txt"
 8 COMMENT_CHARS = '#:'
 9 
10 drawing = Drawing(400, 200)
11 data = []
12 for line in open(r"D:\Python基础教程\第21章 项目2:画幅好画\Predict.txt").readlines():
13     #line = line.decode()  # 解码为Unicode
14     print(line)
15     if not line.isspace() and line[0] not in COMMENT_CHARS:
16         data.append([float(n) for n in line.split()])
17 
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 
23 lp = LinePlot()
24 lp.x = 50
25 lp.y = 50
26 lp.height = 125
27 lp.width = 300
28 lp.data = [list(zip(times, pred)),
29            list(zip(times, high)),
30            list(zip(times, low))]
31 lp.lines[0].strokeColor = colors.blue
32 lp.lines[1].strokeColor = colors.red
33 lp.lines[2].strokeColor = colors.green
34 
35 drawing.add(lp)
36 
37 drawing.add(String(250, 150, 'Sunspots', fontSize=14, fillColor=colors.red))
38 renderPDF.drawToFile(drawing, 'report2.pdf', 'Sunspots')

Predict.txt

 1 :Predicted_Sunspot_Numbers_and_Radio_Flux: Predict.txt
 2 :Created: 2019 Oct 07 0300 UTC
 3 # Prepared by the U.S. Dept. of Commerce, NOAA, Space Weather Prediction Center (SWPC).
 4 # Please send comments and suggestions to [email protected]
 5 #
 6 # Sunspot Number: S.I.D.C. Brussels International Sunspot Number.
 7 # 10.7cm Radio Flux value: Penticton, B.C. Canada.
 8 # Predicted values are based on the consensus of the Solar Cycle 24 Prediction Panel.
 9 #
10 # See the README3 file for further information.
11 #
12 # Missing or not applicable data:  -1
13 #
14 #         Predicted Sunspot Number And Radio Flux Values
15 #                     With Expected Ranges
16 #
17 #         -----Sunspot Number------  ----10.7 cm Radio Flux----
18 # YR MO   PREDICTED    HIGH    LOW   PREDICTED    HIGH    LOW
19 #--------------------------------------------------------------
20 2019 04         2.8     3.8     1.8       69.4    70.4    68.4
21 2019 05         2.9     4.9     0.9       68.9    69.9    67.9
22 2019 06         3.1     6.1     0.1       68.3    70.3    66.3
23 2019 07         3.1     8.1     0.0       67.6    70.6    64.6
24 2019 08         3.2     8.2     0.0       66.9    70.9    62.9
25 2019 09         3.2     9.2     0.0       66.1    70.1    62.1
26 2019 10         3.0    10.0     0.0       65.2    70.2    60.2
27 2019 11         2.8     9.8     0.0       64.4    70.4    60.0
28 2019 12         2.8    10.8     0.0       63.7    70.7    60.0
29 2020 01         2.9    11.9     0.0       63.1    71.1    60.0
30 2020 02         3.1    12.1     0.0       62.6    70.6    60.0
31 2020 03         3.2    13.2     0.0       62.0    71.0    60.0
32 2020 04         3.1    13.1     0.0       61.6    70.6    60.0
33 2020 05         2.8    12.8     0.0       61.3    70.3    60.0
34 2020 06         2.6    12.6     0.0       61.1    70.1    60.0
35 2020 07         2.4    12.4     0.0       60.9    69.9    60.0
36 2020 08         2.2    12.2     0.0       60.7    69.7    60.0
37 2020 09         2.0    12.0     0.0       60.6    69.6    60.0
38 2020 10         1.9    11.9     0.0       60.4    69.4    60.0
39 2020 11         1.7    11.7     0.0       60.3    69.3    60.0
40 2020 12         1.6    11.6     0.0       60.2    69.2    60.0
41 2021 01         1.5    11.5     0.0       60.0    69.0    60.0
42 2021 02         1.3    11.3     0.0       59.9    68.9    60.0
43 2021 03         1.2    11.2     0.0       59.8    68.8    60.0
44 2021 04         1.1    11.1     0.0       59.7    68.7    60.0
45 2021 05         1.0    11.0     0.0       59.6    68.6    60.0
46 2021 06         0.9    10.9     0.0       59.6    68.6    60.0
47 2021 07         0.9    10.9     0.0       59.5    68.5    60.0
48 2021 08         0.8    10.8     0.0       59.4    68.4    60.0
49 2021 09         0.7    10.7     0.0       59.4    68.4    60.0
50 2021 10         0.7    10.7     0.0       59.3    68.3    60.0
51 2021 11         0.6    10.6     0.0       59.2    68.2    60.0
52 2021 12         0.5    10.5     0.0       59.2    68.2    60.0
53 2022 01         0.5    10.5     0.0       59.2    68.2    60.0
54 2022 02         0.4    10.4     0.0       59.1    68.1    60.0
55 2022 03         0.4    10.4     0.0       59.1    68.1    60.0
56 2022 04         0.4    10.4     0.0       59.0    68.0    60.0
57 2022 05         0.3    10.3     0.0       59.0    68.0    60.0
58 2022 06         0.3    10.3     0.0       59.0    68.0    60.0
59 2022 07         0.3    10.3     0.0       59.0    68.0    60.0
60 2022 08         0.2    10.2     0.0       58.9    67.9    60.0
61 2022 09         0.2    10.2     0.0       58.9    67.9    60.0
62 2022 10         0.2    10.2     0.0       58.9    67.9    60.0
63 2022 11         0.2    10.2     0.0       58.9    67.9    60.0
64 2022 12         0.2    10.2     0.0       58.9    67.9    60.0
View Code

猜你喜欢

转载自www.cnblogs.com/landerhu/p/11673086.html