Python项目二:画幅好画

来源:互联网 发布:皇冠淘宝店转让 编辑:程序博客网 时间:2024/04/30 12:05

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

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

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

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

代码如下:

from urllib.request import urlopenfrom reportlab.graphics.charts.piecharts import Piefrom reportlab.lib.colors import darkcyan, blue, cyanfrom reportlab.graphics.charts.legends import Legendfrom reportlab.graphics.shapes import Drawing, _DrawingEditorMixinfrom reportlab.lib.styles import black, whitefrom reportlab.graphics.charts.textlabels import Label#import typesURL = 'http://www.reportlab.com/'lowercaseCount = 0capitalCount = 0numCount = 0otherCount = 0for 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 += 1print(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.15if __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源码,官网上还有很多例子,上面的画饼图原型就来自官网上其他用户捐献代码。

0 0