python生成折线图

图形生成工具包 reportlab (下载地址:https://bitbucket.org/rptlab/reportlab/get/ddf3d4f5066a.zip

数据地址:ftp://ftp.swpc.noaa.gov/pub/weekly/Predict.txt

 

#! /usr/bin/env python
#coding=utf-8
#sunspots_final.py

from urllib import request
from reportlab.graphics.shapes import *
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics import renderPDF

URL='ftp://ftp.swpc.noaa.gov//pub//weekly//Predict.txt'
COMMENT_CHARS='#:'

drawing=Drawing(400,200)
data=[]
for line in request.urlopen(URL,'r').readlines():
    line=line.decode('utf-8')
    if not line.isspace() and not line[0] in COMMENT_CHARS:
        data.append([float(n) for n in line.split()])
        
pred=[row[5]-40 for row in data]
high=[row[6]-40 for row in data]
low=[row[7]-40 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,'SunSopts',fontSize=14,fillColor=colors.orange))

renderPDF.drawToFile(drawing,'report2.pdf','SunSpots')
        
#print(data)
        

 

posted on 2017-09-03 14:58  风清扬dyf  阅读(2917)  评论(0编辑  收藏  举报

导航