python练习二—画幅好画

这个练习相对简单,主要是对reportlab库的学习调用,看代码

#! /usr/bin/env pyton
# -*- coding=utf-8 -*-

from reportlab.lib import colors
from reportlab.graphics.shapes import *
from reportlab.graphics import renderPDF
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
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(400, 200)

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 = [zip(times, pred), zip(times, high), zip(times, low)]
lp.lines[0].strokeColor = colors.blue
lp.lines[1].strokeColor = colors.red
lp.lines[2].strokeColor = colors.green

drawing.add(lp)

renderPDF.drawToFile(drawing, 'sunspots.pdf','sunspot')

#zip使用

a = list(1,2)
b = list(3,4)
d = zip(a,b)=[(1,3), (2,4)]

# 列表和元组

列表是[],元组是()
列表长度可变,元组不可变

# 列表推导式,轻量级循环
l = [row for row in rows]

 


 

完整代码

http://pan.baidu.com/s/1nvtPDH3

posted @ 2016-05-07 14:22  lacker  阅读(766)  评论(0编辑  收藏  举报