python生成折线图,,图形生成工具包 re


图形生成工具包 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.pyfrom urllib import requestfrom reportlab.graphics.shapes import *from reportlab.graphics.charts.lineplots import LinePlotfrom reportlab.graphics.charts.textlabels import Labelfrom reportlab.graphics import renderPDFURL=‘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=50lp.height=125#轴的长度lp.width=300lp.data=[list(zip(times,pred)),list(zip(times,high)),list(zip(times,low))]lp.lines[0].strokeColor=colors.bluelp.lines[1].strokeColor=colors.redlp.lines[2].strokeColor=colors.greendrawing.add(lp)drawing.add(String(250,150,‘SunSopts‘,fontSize=14,fillColor=colors.orange))renderPDF.drawToFile(drawing,‘report2.pdf‘,‘SunSpots‘)        #print(data)        

python生成折线图

评论关闭