python使用reportlab畫圖樣本(含中文漢字)

來源:互聯網
上載者:User
準備工作

開發環境: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()

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.