Python uses reportlab for drawing examples
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 ()