Created
August 20, 2021 02:58
-
-
Save TSKGunGun/83a058f1e3ed84b73130c1fd61e1de41 to your computer and use it in GitHub Desktop.
Python基礎&実践プログラミング Ch21.5 swpcのデータがtxtではなくjson形式に変わっていたため、修正コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import urlopen | |
from reportlab.graphics.shapes import * | |
from reportlab.graphics.charts.lineplots import LinePlot | |
from reportlab.graphics.charts.textlabels import Label | |
from reportlab.graphics import renderPDF | |
import json | |
URL = "https://services.swpc.noaa.gov/json/solar-cycle/predicted-solar-cycle.json" | |
drawing = Drawing(400, 200) | |
data = [] | |
sunSpotData = json.load(urlopen(URL)) | |
for row in sunSpotData: | |
year = row['time-tag'][0:4] | |
month = row['time-tag'][-2:] | |
item = (int(year), int(month), float(row['predicted_ssn']), float(row['high_ssn']), float(row['low_ssn']), | |
float(row['predicted_f10.7']), float(row['high_f10.7']), float(row['low_f10.7'])) | |
data.append(item) | |
pred = [row[2] for row in data] | |
high = [row[3] for row in data] | |
low = [row[4] for row in data] | |
times = [row[0] + row[1]/12.0 for row in data] | |
lp = LinePlot() | |
lp.x = 50 | |
lp.y = 50 | |
lp.height = 125 | |
lp.width = 300 | |
lp.data = [list(zip(times, pred)), | |
list(zip(times, high)), | |
list(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, 'report2.pdf', 'Sunspots') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment