python绘图工具reportlab介绍----续
来源:互联网 发布:淘宝店铺号 编辑:程序博客网 时间:2024/06/06 18:33
上一篇转载<python绘图工具reportlab介绍>,直接运行代码有些问题,慢慢解决了,记录下.
1,坐标图
百度资料:
http://blog.sina.com.cn/s/blog_53a8a9d40101krae.html
http://hulianwang2014.iteye.com/blog/2048674
改代码如下:
#coding:utf-8"""http://blog.sina.com.cn/s/blog_53a8a9d40101krae.htmlhttp://hulianwang2014.iteye.com/blog/2048674"""from urllib import urlopenfrom reportlab.graphics.shapes import *from reportlab.graphics.charts.lineplots import LinePlotfrom reportlab.graphics.charts.textlabels import Labelfrom reportlab.graphics import renderPDF# URL = 'http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt' #将文件URL保存在变量URL中# COMMENT_CHARS = '#:' #变量COMMENT_CHARS设定为“#:”##drawing = Drawing(400, 200) # 创建一个400*200像素大小 的PDF格式图形# data = []# #读取Product中数据进行筛选,将正确数据添加到data元组中# for line in urlopen(URL).readlines():# if not line.isspace() and not line[0] in COMMENT_CHARS:# data.append([float(n) for n in line.split()])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),]#获取data元组中对应数据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]#LinePlot类实例化不需要任务参数,设置主要特性是:x。y、height、width和data,data点的坐标列表。lp = LinePlot()lp.x = 50lp.y = 50lp.height = 125lp.width = 300lp.data = [zip(times, pred), zip(times, high), zip(times, low)]#为每条线调置笔画颜色lp.lines[0].strokeColor = colors.bluelp.lines[1].strokeColor = colors.redlp.lines[2].strokeColor = colors.greendrawing.add(lp) # 将设定的LinePlost类添加到drawingdrawing.add(String(250, 150, 'Sunspots', fontSize=14, fillColor=colors.red)) # 添加一个String对象到drawing中#renderPDF.drawToFile是把内容保存到report2.pdfrenderPDF.drawToFile(drawing, '/home/cloud/temp/report2.pdf')#, 'Sunspots')对比下上篇中的代码,其中网址已挂,所以直接用数据代替;
drawing.add中'Sunspots'参数好像才能写入图中.
2,坐标柱状图
from standard_colors import pdf_chart_colors, setItems这句话一头雾水,注释相关代码后得到的柱状图颜色只有两三种,和范例不同.
谷歌得到:http://stackoverflow.com/questions/6286531/error-in-import-statement-in-reportlab ,发现可能是个模块,转载时没有记录:
standard_colors.py
from reportlab.lib.colors import HexColorpdf_chart_colors = [ HexColor("#0000e5"), HexColor("#1f1feb"), HexColor("#5757f0"), HexColor("#8f8ff5"), HexColor("#c7c7fa"), HexColor("#f5c2c2"), HexColor("#eb8585"), HexColor("#e04747"), HexColor("#d60a0a"), HexColor("#cc0000"), HexColor("#ff0000"), ]def setItems(n, obj, attr, values): m = len(values) i = m // n for j in xrange(n): setattr(obj[j],attr,values[j*i % m])
运行还是报错:
reportlab.graphics.renderPM.RenderPMError: Can't setFont(Times-Roman) missing the T1 files?
Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be string, not None
测试+百度后,发现是默认的Times-Roman字体没找到,所以自己下载个字体wqy-microhei-lite.ttc注册为Times-Roman:
import reportlab.pdfbase.ttfontsreportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('Times-Roman', '/home/cloud/IdeaProjects/python_test/wqy-microhei-lite.ttc'))代码中的Helvetica和Helvetica-Bold也可以改为Times-Roman,因为我的电脑也没这些字体.
3,饼图
同上,没有Times-Roman,注册之:
import reportlab.pdfbase.ttfontsreportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('Times-Roman', '/home/cloud/IdeaProjects/python_test/wqy-microhei-lite.ttc'))因为我下载的是中文字体,所以图标中'Label 1'之类的可以改用中文.
0 0
- python绘图工具reportlab介绍----续
- python绘图工具reportlab介绍
- python绘图工具reportlab介绍
- Python之ReportLab绘图
- windows: python 安装 reportlab 工具
- Web绘图工具介绍
- mac 安装reportlab工具
- python之reportlab
- Python使用ReportLab画图
- python之reportlab
- Python reportlab教程
- Python reportLab 生成PDF
- Python--reportlab 下载
- Python绘图介绍
- python reportlab 生成pdf (一)
- Python Reportlab 中Flowables 对象
- Python之ReportLab绘制文本
- python-reportlab饼图绘制
- LVS
- ffmpeg重要结构体之HEVCContext和HEVCFrame
- 文件与文件系统的压缩与打包 tar gzip bzip2
- 常用算法一(分治算法)
- React 加载器 解说
- python绘图工具reportlab介绍----续
- overview_How do I develop a client
- highChartsjs饼状图
- php url decode问题:+被转义成空格问题
- Windows 7 Vs2012配置OpenGL出现的问题
- 按bean的属性值对list集合进行排序
- imshow的输入数据格式
- 欢迎使用CSDN-markdown编辑器
- scrapy命令行工具