Python uses Reportlab to draw a diagram example

Source: Internet
Author: User

  This article mainly introduces Python using Reportlab diagram example, we refer to the use of the bar

Preparation   Development environment: Python2.6,reportlab   Preparing Chinese font files: SIMSUN.TTC   code:     Code as follows: #!/usr/bin/env python2.6 # Coding:utf-8   Import traceback   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 & nbsp #注意data的类型, #每一个数据点是一个元组 #一条曲线对应一个存储数据点元组的元组 #一个图形可以包含多条曲线, using a list of data=[((1,100), (2,200), (3,300), (4,400), (5,500 )), ((1,50), (2,80), (3,400), (4,40), (5,70))]   drawing = Drawing (M)   LP = Lineplot () lp.x = #坐标轴中心坐标 LP. y = lp.height = Lp.width = Lp.data = Data Lp.joinedlines = 1 Lp.lines.symbol = Makemarker (' filledcircle ') &nbsp ; Lp.xValueAxis.valueMin = 1 Lp.xValueAxis.valueMax = 5 Lp.xValueAxis.valueStep = 1   lp.yValueAxis.valueMin = 0 Lp.yva Lueaxis.valuemax = lp.yvalueaxis.vAluestep = DRAWING.ADD (LP)   title = Label () #若需要显示中文, you need to first register a Chinese font pdfmetrics.registerfont (ttfonts. Ttfont ("haha", "SIMSUN.TTC")) title.fontname   = "haha" title.fontsize   = Title_text = Unicode (' Hello ', ' GBK ') # Title_text = "abc" Title._text = Title_text title.x          = title.y       & nbsp  = 280 title.textanchor = ' middle ' drawing.add (title)   Xlabel = Label () Xlabel._text = ' x ' xlabel.fontsize &nbsp ; = xlabel.x          = xlabel.y          = xlabel.textanchor = ' Middle ' drawing.add (xlabel)   Ylabel = Label () Ylabel._text = "Y" ylabel.fontsize   = ylabel.x          = ylabel.y          = 295 ylabel.textanchor = ' Middle ' drawing.add (Ylabel)   Try:      drawing.save (formats=[' gif '],outdir= ", fnroot=" abc ") except:       Traceback.print_exc ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.