#!/usr/bin/env python
# -*- 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))
# -*- 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')
本文介绍了一个使用Python和ReportLab库绘制太阳黑子活动周期图表的简单程序。该程序从给定的数据集中读取每年每月太阳黑子的预测数量、最高值和最低值,并将这些数据以线条图的形式展示出来。

被折叠的 条评论
为什么被折叠?



