开发者社区> 余二五> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

Python:使用pycha快速绘制办公常用图(饼图、垂直直方图、水平直方图、散点图等七种图形)

简介:
+关注继续查看
福利推荐:阿里云、腾讯云、华为云等大品牌云产品全线2折优惠活动来袭,4核8G云服务器899元/3年,新老用户共享优惠,点击这里立即抢购>>>

一、代码:


[python]?view plaincopy

  1. #!/usr/bin/env?python??

  2. #?-*-?coding:?utf-8?-*-??

  3. ??

  4. import?cairo???

  5. import?pycha.pie??

  6. import?pycha.bar??

  7. import?pycha.scatter??

  8. import?pycha.stackedbar??

  9. import?pycha.line??

  10. ??

  11. #设置画布??

  12. def?set_charvalue():??

  13. ????width,height=600,600???

  14. ????surface=cairo.ImageSurface(cairo.FORMAT_ARGB32,width,height)???

  15. ????return?surface??

  16. ??????

  17. #画饼图??

  18. def?draw_pie(surface,?options,?dataSet):??

  19. ????chart=pycha.pie.PieChart(surface,options)???

  20. ????chart.addDataset(dataSet)???

  21. ????chart.render()???

  22. ????surface.write_to_png('d:\\Pie.png')???

  23. ??

  24. #垂直直方图??

  25. def?draw_vertical_bar(surface,?options,?dataSet):??

  26. ????chart=pycha.bar.VerticalBarChart(surface,options)???

  27. ????chart.addDataset(dataSet)???

  28. ????chart.render()???

  29. ????surface.write_to_png('d:\\vertical_bar.png')?????

  30. ???

  31. #垂直水平直方图??????

  32. def?draw_horizontal_bar(surface,?options,?dataSet):??

  33. ????chart?=?pycha.bar.HorizontalBarChart(surface,options)???

  34. ????chart.addDataset(dataSet)???

  35. ????chart.render()???

  36. ????surface.write_to_png('d:\\horizontal_bar.png')?????

  37. ??????

  38. #线图??????

  39. def?draw_line(surface,?options,?dataSet):??

  40. ????chart?=?pycha.line.LineChart(surface,options)???

  41. ????chart.addDataset(dataSet)???

  42. ????chart.render()???

  43. ????surface.write_to_png('d:\\line.png')????????

  44. ??

  45. #点图??????

  46. def?draw_scatterplot(surface,?options,?dataSet):??

  47. ????chart?=?pycha.scatter.ScatterplotChart(surface,options)???

  48. ????chart.addDataset(dataSet)???

  49. ????chart.render()???

  50. ????surface.write_to_png('d:\\scatterplotChart.png')???????????

  51. ??

  52. #垂直块图???????

  53. def?draw_stackedverticalbarChar(surface,?options,?dataSet):??

  54. ????chart?=?pycha.stackedbar.StackedVerticalBarChart(surface,options)???

  55. ????chart.addDataset(dataSet)???

  56. ????chart.render()???

  57. ????surface.write_to_png('d:\\stackedVerticalBarChart.png')????????

  58. ??

  59. #水平块图??

  60. def?draw_stackedhorizontalbarChart(surface,?options,?dataSet):??

  61. ????chart?=?pycha.stackedbar.StackedHorizontalBarChart(surface,options)???

  62. ????chart.addDataset(dataSet)???

  63. ????chart.render()???

  64. ????surface.write_to_png('d:\\stackedhorizontalbarChart.png')??????

  65. ??????

  66. if?__name__?==?'__main__':??

  67. ????'''''?

  68. ????Function:使用pycha画各种图表?

  69. ????Input:NONE?

  70. ????Output:?NONE?

  71. ????author:?socrates?

  72. ????blog:http://blog.csdn.net/dyx1024?

  73. ????date:2012-02-28?

  74. ????'''??

  75. ????#数据来源??

  76. ????dataSet=(???

  77. ?????????????('iphone',((0,1),(1,3),(2,2.5))),???

  78. ?????????????('htc',((0,2),(1,4),(2,3))),???

  79. ?????????????('hw',((0,5),(1,1,),(2,0.5))),???

  80. ?????????????('zte',((0,3),(1,2,),(2,1.5))),???

  81. ????????????)???

  82. ??????

  83. ????#图像属性定义??

  84. ????options={???

  85. ????????????????'legend':{'hide':False},???

  86. ????????????????'title':'手机销售量分布图(by?dyx1024)',??

  87. ????????????????'titleColor':'#0000ff',??

  88. ????????????????'titleFont':'字体',??

  89. ????????????????'background':{'chartColor':?'#ffffff'},???

  90. ????????????????'axis':{'labelColor':'#ff0000'},??

  91. ????????????}???????

  92. ??????

  93. ??????

  94. ????surface?=?set_charvalue()??

  95. ??????

  96. ????#根据需要调用不同函数画不同形状的图??

  97. ????#draw_pie(surface,?options,?dataSet)??

  98. ????#draw_vertical_bar(surface,?options,?dataSet)??

  99. ????#draw_horizontal_bar(surface,?options,?dataSet)??

  100. ????#draw_scatterplot(surface,?options,?dataSet)??

  101. ????#draw_stackedverticalbarChar(surface,?options,?dataSet)??

  102. ????#draw_stackedhorizontalbarChart(surface,?options,?dataSet)??

  103. ????draw_line(surface,?options,?dataSet)??

  104. ??????

  105. ??????????

[python]?view plaincopy

  1. #!/usr/bin/env?python??

  2. #?-*-?coding:?utf-8?-*-??

  3. ??

  4. import?cairo???

  5. import?pycha.pie??

  6. import?pycha.bar??

  7. import?pycha.scatter??

  8. import?pycha.stackedbar??

  9. import?pycha.line??

  10. ??

  11. #设置画布??

  12. def?set_charvalue():??

  13. ????width,height=600,600???

  14. ????surface=cairo.ImageSurface(cairo.FORMAT_ARGB32,width,height)???

  15. ????return?surface??

  16. ??????

  17. #画饼图??

  18. def?draw_pie(surface,?options,?dataSet):??

  19. ????chart=pycha.pie.PieChart(surface,options)???

  20. ????chart.addDataset(dataSet)???

  21. ????chart.render()???

  22. ????surface.write_to_png('d:\\Pie.png')???

  23. ??

  24. #垂直直方图??

  25. def?draw_vertical_bar(surface,?options,?dataSet):??

  26. ????chart=pycha.bar.VerticalBarChart(surface,options)???

  27. ????chart.addDataset(dataSet)???

  28. ????chart.render()???

  29. ????surface.write_to_png('d:\\vertical_bar.png')?????

  30. ???

  31. #垂直水平直方图??????

  32. def?draw_horizontal_bar(surface,?options,?dataSet):??

  33. ????chart?=?pycha.bar.HorizontalBarChart(surface,options)???

  34. ????chart.addDataset(dataSet)???

  35. ????chart.render()???

  36. ????surface.write_to_png('d:\\horizontal_bar.png')?????

  37. ??????

  38. #线图??????

  39. def?draw_line(surface,?options,?dataSet):??

  40. ????chart?=?pycha.line.LineChart(surface,options)???

  41. ????chart.addDataset(dataSet)???

  42. ????chart.render()???

  43. ????surface.write_to_png('d:\\line.png')????????

  44. ??

  45. #点图??????

  46. def?draw_scatterplot(surface,?options,?dataSet):??

  47. ????chart?=?pycha.scatter.ScatterplotChart(surface,options)???

  48. ????chart.addDataset(dataSet)???

  49. ????chart.render()???

  50. ????surface.write_to_png('d:\\scatterplotChart.png')???????????

  51. ??

  52. #垂直块图???????

  53. def?draw_stackedverticalbarChar(surface,?options,?dataSet):??

  54. ????chart?=?pycha.stackedbar.StackedVerticalBarChart(surface,options)???

  55. ????chart.addDataset(dataSet)???

  56. ????chart.render()???

  57. ????surface.write_to_png('d:\\stackedVerticalBarChart.png')????????

  58. ??

  59. #水平块图??

  60. def?draw_stackedhorizontalbarChart(surface,?options,?dataSet):??

  61. ????chart?=?pycha.stackedbar.StackedHorizontalBarChart(surface,options)???

  62. ????chart.addDataset(dataSet)???

  63. ????chart.render()???

  64. ????surface.write_to_png('d:\\stackedhorizontalbarChart.png')??????

  65. ??????

  66. if?__name__?==?'__main__':??

  67. ????'''''?

  68. ????Function:使用pycha画各种图表?

  69. ????Input:NONE?

  70. ????Output:?NONE?

  71. ????author:?socrates?

  72. ????blog:http://blog.csdn.net/dyx1024?

  73. ????date:2012-02-28?

  74. ????'''??

  75. ????#数据来源??

  76. ????dataSet=(???

  77. ?????????????('iphone',((0,1),(1,3),(2,2.5))),???

  78. ?????????????('htc',((0,2),(1,4),(2,3))),???

  79. ?????????????('hw',((0,5),(1,1,),(2,0.5))),???

  80. ?????????????('zte',((0,3),(1,2,),(2,1.5))),???

  81. ????????????)???

  82. ??????

  83. ????#图像属性定义??

  84. ????options={???

  85. ????????????????'legend':{'hide':False},???

  86. ????????????????'title':'手机销售量分布图(by?dyx1024)',??

  87. ????????????????'titleColor':'#0000ff',??

  88. ????????????????'titleFont':'字体',??

  89. ????????????????'background':{'chartColor':?'#ffffff'},???

  90. ????????????????'axis':{'labelColor':'#ff0000'},??

  91. ????????????}???????

  92. ??????

  93. ??????

  94. ????surface?=?set_charvalue()??

  95. ??????

  96. ????#根据需要调用不同函数画不同形状的图??

  97. ????#draw_pie(surface,?options,?dataSet)??

  98. ????#draw_vertical_bar(surface,?options,?dataSet)??

  99. ????#draw_horizontal_bar(surface,?options,?dataSet)??

  100. ????#draw_scatterplot(surface,?options,?dataSet)??

  101. ????#draw_stackedverticalbarChar(surface,?options,?dataSet)??

  102. ????#draw_stackedhorizontalbarChart(surface,?options,?dataSet)??

  103. ????draw_line(surface,?options,?dataSet)??

  104. ??????

  105. ??????????



二、测试:

1、函数draw_pie(surface, options, dataSet):

?

2、函数draw_vertical_bar(surface, options, dataSet):

3、函数draw_horizontal_bar(surface, options, dataSet):

4、函数draw_line(surface, options, dataSet):

5、函数draw_scatterplot(surface, options, dataSet):

6、函数draw_stackedverticalbarChar(surface, options, dataSet):

7、函数draw_stackedhorizontalbarChart(surface, options, dataSet):










本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1576046,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Python第四讲——使用IPython/Jupyter Notebook与日志服务玩转超大规模数据分析与可视化
立即下载
Python 系列直播——深入Python与日志服务,玩转大规模数据分析处理实战第二讲
立即下载
Python第五讲——关于爬虫如何做js逆向的思路
立即下载


http://www.vxiaotou.com