Python项目二:画幅好画

分析python基础教程(第二版)中的项目2

本项目使用reportlab实现基础的画图操作,画图数据从网上获取,分析后画出曲线,并保存到PDF文件中。
由于网上的黑子数据与书中所说的不一样,所以略作修改,获取一个网页,然后统计这个网页上数字,小写字母,大写字母,以及其他符号的个数,以这些数字画一个饼图,并输入到PNG以及PDF中。

 代码地址:https://code.csdn.net/ranky2009/pythonsmallproject

实例:
运行之后,饼图如下:

代码如下:

from urllib.request import urlopen
from reportlab.graphics.charts.piecharts import Pie
from reportlab.lib.colors import darkcyan, blue, cyan
from reportlab.graphics.charts.legends import Legend
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
from reportlab.lib.styles import black, white
from reportlab.graphics.charts.textlabels import Label

#import types
URL = 'http://www.reportlab.com/'

lowercaseCount = 0
capitalCount = 0
numCount = 0
otherCount = 0

for line in urlopen(URL).readlines():
    if not line.isspace():
        for char in line:
            #print(type(char))
            tempChar = chr(char)
            if tempChar >= '0' and tempChar <= '9':
                numCount += 1
            elif tempChar >='a' and tempChar <= 'z':
                lowercaseCount += 1
            elif tempChar >='A' and tempChar <='Z':
                capitalCount += 1
            else:
                otherCount += 1

print(numCount, lowercaseCount, capitalCount, otherCount)

class Drawing_Pie(_DrawingEditorMixin,Drawing):
    def __init__(self,width=400,height=400,*args,**kw):
        Drawing.__init__(self,width,height,*args,**kw)
        self._add(self,Pie(),name=None,validate=None,desc=None)

        # Set the size and location of the chart
        self.contents[0].width             = 240
        self.contents[0].height            = 240
        self.contents[0].y                 = 80
        self.contents[0].x                 = 80

        # Fill in the chart with sample data and labels
        self.contents[0].data              = [numCount, lowercaseCount, capitalCount, otherCount]
        self.contents[0].labels            = ['Number','Lowercase','Captital','Other']

        # Make the slices pop out and add a border to the slices
        self.contents[0].slices.popout                    = 2
        self.contents[0].slices.strokeWidth               = 1


        # Set the font size and position the label
        self.contents[0].slices.fontSize                  = 14
        self.contents[0].slices.labelRadius               = 1.25

        # Turn off simple labels so we can use customized labels
        self.contents[0].simpleLabels                     = 0
        # Define a simple pointer for all labels
        self.contents[0].slices.label_simple_pointer      = 1

        # Define a first label with a black border,
        # white text and dark cyan background
        self.contents[0].slices[0].label_boxStrokeColor   = black
        self.contents[0].slices[0].fontColor              = white       
        self.contents[0].slices[0].label_boxFillColor     = darkcyan
        # Change the anchor point of the label box
        self.contents[0].slices[0].label_boxAnchor        = 's'

        # Turn off pointer for the third label, change the text color
        self.contents[0].slices[2].label_simple_pointer   = 0
        self.contents[0].slices[2].fontColor              = blue
        # and position it closer to the chart
        self.contents[0].slices[2].labelRadius            = 1.05

        # Turn off pointer for the fourth label
        # and position it within the chart
        self.contents[0].slices[3].label_simple_pointer  = 0
        self.contents[0].slices[3].label_height          = 22
        self.contents[0].slices[3].labelRadius           = 1.15
if __name__=="__main__": #NORUNTESTS
    Drawing_Pie().save(formats=['pdf'],outDir='.',fnRoot='pie_figure')
    Drawing_Pie().save(formats=['png'],outDir='.',fnRoot='pie_figure')


由于代码比较容易看懂,不做详细分析。
Note:在官网http://www.reportlab.com/上下载reportlab的python源码,官网上还有很多例子,上面的画饼图原型就来自官网上其他用户捐献代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值