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
- python项目练习二:画幅好画
- python项目练习二:画幅好画
- Python项目二:画幅好画
- python项目练习二:画幅好画
- python十个项目之----画幅好画
- Python项目之画幅好画
- python基础教程:画幅好画
- python基础教程里的项目2:画幅好画
- python学习(一):画幅好画
- 画幅好画
- Python基础教程实践2,画幅好画(win7,64位系统)
- Python基础教程实践2,画幅好画(win7,64位系统)
- 好项目离不开好插件(二)
- python实践项目(二)
- 我要用css画幅画(九) - Apple Logo
- 全画幅与半画幅
- python小项目二:图片转字符画
- 相机画幅小节
- 在Mac下Eclipse创建Android工程失败的解决
- 《C语言及程序设计》实践项目——结构体应用
- [Leetcode]-Minimum Depth of Binary Tree
- Python使用cx_Oracle模块连接操作Oracle数据库
- sigaction 函数
- Python项目二:画幅好画
- SQL Server数据库ROW_NUMBER()函数使用详解
- 省级城市主外键建立的SQL
- arm-linux-gcc交叉编译器安装说明
- 排序 - 冒泡法(改进)
- input属性placeholder的兼容性问题
- js七个全局函数
- C++ 类的反汇编
- 【leetcode】Populating Next Right Pointers in Each Node I&&II