# -*- coding: utf-8 -*-
#python基礎教程太陽黑子圖形程序的第一個原型(sunspots_roto.py)
from reportlab.lib import colors
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPDF
'''
data = [
# Year Month Predicted High Low
(2016 ,8, 86.0, 87.0, 85.0),
(2016 ,9, 85.1, 86.1, 84.1),
(2016 ,10, 84.5, 86.5, 82.5),
(2016 ,11, 83.8, 86.8, 80.8),
(2016 ,12, 83.4, 87.4, 79.4),
(2017 ,1, 83.2, 87.2, 79.2),
(2017 ,2, 82.9, 87.9, 77.9),
(2017 ,3, 82.3, 88.3, 76.3),
(2017 ,4, 81.6, 88.6, 74.6),
(2017 ,5, 81.3, 89.3, 73.3)
]
'''
data = [
# Year Month Predicted High Low
(2007, 8, 113.2, 114.2, 112.2),
(2007, 9, 112.8, 115.8, 109.8),
(2007, 10, 111.0, 116.0, 106.0),
(2007, 11, 109.8, 116.8, 102.8),
(2007, 12, 107.3, 115.3, 99.3),
(2008, 1, 105.2, 114.2, 96.2),
(2008, 2, 104.1, 114.1, 94.1),
(2008, 3, 99.9, 110.9, 88.9),
(2008, 4, 94.8, 106.8, 82.8),
(2008, 5, 91.2, 104.2, 78.2),
]
drawing = Drawing(200, 150)
pred = [row[2]-40 for row in data]
high = [row[3]-40 for row in data]
low = [row[4]-40 for row in data]
times = [200*((row[0] + row[1]/12.0) - 2007)-110 for row in data]
"""
教程原文 drawing.add(PolyLine(zip(times, pred), strokeColor = colors.blue)
用python3顯示 TypeError: object of type 'zip' has no len() 錯誤
棧溢出 給出答案是
這是Python 2和Python 3之間的問題。在Python 2使用shuffle後的zip工作,因爲zip返回一個列表。
在Python 3:“TypeError:類型'zip'的對象沒有len()”,因爲zip在Python 3中返回一個迭代器。<class 'zip'>
解決方案,使用list()轉換爲列表
http://stackoverflow.com/questions/31011631/python-2-3-object-of-type-zip-has-no-len
http://stackoverflow.com/questions/18048310/len-error-for-zipping-in-python
"""
print(list((zip(times, pred))))
print(zip(times, pred))
drawing.add(PolyLine((zip(times, pred)), strokeColor = colors.blue))
drawing.add(PolyLine((zip(times, high)), strokeColor = colors.red))
drawing.add(PolyLine((zip(times, low)), strokeColor = colors.green))
drawing.add(String(45,115 , ' Sunpots items22.py', fontSize = 15, fillColor = colors.red))
renderPDF.drawToFile(drawing, '/home/buntu/py5/items22.pdf', 'Title Sunspots')