python使用reportlab画图示例(含中文汉字)
时间:2022-10-22 17:14:18
准备工作
开发环境:python2.6,reportlab
准备中文字体文件:simsun.ttc
代码:
#!/usr/bin/env python2.6
#coding:utf-8
import traceback
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
from reportlab.graphics import renderPDF
from reportlab.graphics.widgets.markers import makeMarker
from reportlab.pdfbase import pdfmetrics, ttfonts
#注意data的类型,
#每一个数据点是一个元组
#一条曲线对应一个存储数据点元组的元组
#一个图形可以包含多条曲线,用列表存储曲线元组
data=[((1,100),(2,200),(3,300),(4,400),(5,500)),((1,50),(2,80),(3,400),(4,40),(5,70))]
drawing = Drawing(500, 300)
lp = LinePlot()
lp.x = 50 #坐标轴中心坐标
lp.y = 30
lp.height = 250
lp.width = 400
lp.data = data
lp.joinedLines = 1
lp.lines.symbol = makeMarker('FilledCircle')
lp.xValueAxis.valueMin = 1
lp.xValueAxis.valueMax = 5
lp.xValueAxis.valueStep = 1
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 500
lp.yValueAxis.valueStep = 100
drawing.add(lp)
title = Label()
#若需要显示中文,需要先注册一个中文字体
pdfmetrics.registerFont(ttfonts.TTFont("haha", "simsun.ttc"))
title.fontName = "haha"
title.fontSize = 12
title_text = unicode('你好','gbk')
#title_text = "abc"
title._text = title_text
title.x = 250
title.y = 280
title.textAnchor ='middle'
drawing.add(title)
Xlabel = Label()
Xlabel._text = 'x'
Xlabel.fontSize = 12
Xlabel.x = 480
Xlabel.y = 30
Xlabel.textAnchor ='middle'
drawing.add(Xlabel)
Ylabel = Label()
Ylabel._text = "y"
Ylabel.fontSize = 12
Ylabel.x = 40
Ylabel.y = 295
Ylabel.textAnchor ='middle'
drawing.add(Ylabel)
try:
drawing.save(formats=['gif'],outDir=".",fnRoot="abc")
except:
traceback.print_exc()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中for循环的多种使用实例
![](https://img.aspxhome.com/file/2023/5/99225_0s.png)
Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
Python 迭代器Iterator详情
python openvc 裁剪、剪切图片 提取图片的行和列
![](https://img.aspxhome.com/file/2023/4/77874_0s.jpg)
Python中标准库OS的常用方法总结大全
sql Set IDENTITY_INSERT的用法
网页标准化-CSS命名规划整理
python 包之 Pillow 图像处理教程分享
asp如何读取Windows的信息文件(.ini)?
SQL中limit函数语法与用法(MYSQL获取限制某行数据)
经典分享MySQL的limit查询优化
15款非常有用的前端开发CSS网格(grid system)生成器
![](https://img.aspxhome.com/file/UploadPic/20097/6/grid-layout-generator-by-pagecolumn-14s.jpg)
Vue3响应式对象Reactive和Ref的用法解读
uploadify在Firefox下丢失session问题的解决方法
MySQL窗口函数OVER使用示例详细讲解
简单实现SQLServer转MYSQL的方法
![](https://img.aspxhome.com/file/2023/2/93062_0s.jpg)
python 高效去重复 支持GB级别大文件的示例代码
Pyhon Flask框架:第一个Flask程序
![](https://img.aspxhome.com/file/2023/3/81833_0s.png)
python日志logging模块使用方法分析
![](https://img.aspxhome.com/file/2023/2/103802_0s.png)
python多进程基础详解
![](https://img.aspxhome.com/file/2023/9/88969_0s.jpg)