Python uses reportlab for drawing examples (including Chinese characters)

Source: Internet
Author: User
This article describes how to use reportlab in python. Preparations

Development Environment: python2.6, reportlab

Prepare a Chinese font file: simsun. ttc

Code:

The code is as follows:


#! /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 reportlabw.base import into metrics, ttfonts

# Note the data type,
# Each data point is a tuples
# A curve corresponds to a tuple storing data point tuples
# A chart can contain multiple curves and store the curve tuples in the list.
Data = [(1,100), (2,200), (3,300), (4,400), (5,500), (3,400 ), (), ()]

Drawing = Drawing (500,300)

Lp = LinePlot ()
Lp. x = 50 # coordinate axis Center
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 ()
# To display Chinese characters, you must register a Chinese font.
Using metrics. 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 t:
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.