【记录】折腾Python中的一个绘制图表的模块:pycha

背景

看到有人提到pycha,说是关于用python画图表的。

之前也已经折腾过一个类似的,操作Excel中去画图表的:

【已解决】Python中处理操作Excel中的图表(Chart,Graph)

现在这个,貌似是独立的,画图表的。

pycha简介

根据Python官网

http://pypi.python.org/pypi/pycha/

和其主页

https://bitbucket.org/lgs/pycha/wiki/Home

的介绍:

pycha是啥

pycha是个Python模块,用来画图表的的模块。

pycha是PYthon CHArts的缩写;

 

一些例子:

piechart

vbarchart

hbarchart-fixed

linechart

 

pycha的优点/特点

  • 轻量级
  • 用起来简单
  • 使用默认值,画出来的效果就已经很好看了
  • 可以自定义,定制化
  • 没打算支持所有类型的图表,但是尽量支持最常见的,最通用的图表

 

pycha的相关介绍

1.其用到了Cairo库。

2. 与此类似的模块,还有:pyCairoChart

3. Pycha基于Plotr,而Plotr又基于PlotKit

之前其是用javascript实现的(运行在客户端),作者希望其可以在服务端运行,所以将Plotr移植成用Python实现。

4. 官网文档:http://packages.python.org/pycha

 

下载pycha

http://pypi.python.org/pypi/pycha/

中就可以找到下载地址:

pycha-0.6.0.tar.gz

下载得到pycha-0.6.0.tar.gz,解压,得到文件夹pycha-0.6.0。

 

安装pycha

安装之前,发现此处依赖的cairo,还没安装,肯定待会使用也是不行的。

所以先去折腾安装cairo,详见:

【记录】安装Python版本的cairo模块:Pycairo

安装完毕了python版本的cairo,Pycairo后,再去继续安装pycha。

 

打开cmd,切换到对应目录pycha-0.6.0,去执行setup.py install,此处竟然出现没有setuptools的错误。

详细折腾过程参见:

【已解决】Python中使用setup.py install去安装别的模块结果出错:ImportError: No module named setuptools

然后再去安装,就正常了:

D:\tmp\dev_tools\python\pycha\dist\pycha-0.6.0>setup.py install
running install
running bdist_egg
running egg_info
writing pycha.egg-info\PKG-INFO
writing top-level names to pycha.egg-info\top_level.txt
writing dependency_links to pycha.egg-info\dependency_links.txt
writing entry points to pycha.egg-info\entry_points.txt
reading manifest file 'pycha.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'pycha.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\pycha
copying pycha\bar.py -> build\lib\pycha
copying pycha\chart.py -> build\lib\pycha
copying pycha\color.py -> build\lib\pycha
copying pycha\line.py -> build\lib\pycha
copying pycha\pie.py -> build\lib\pycha
copying pycha\scatter.py -> build\lib\pycha
copying pycha\stackedbar.py -> build\lib\pycha
copying pycha\utils.py -> build\lib\pycha
copying pycha\__init__.py -> build\lib\pycha
creating build\lib\chavier
copying chavier\app.py -> build\lib\chavier
copying chavier\dialogs.py -> build\lib\chavier
copying chavier\gui.py -> build\lib\chavier
copying chavier\__init__.py -> build\lib\chavier
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\chavier
copying build\lib\chavier\app.py -> build\bdist.win-amd64\egg\chavier
copying build\lib\chavier\dialogs.py -> build\bdist.win-amd64\egg\chavier
copying build\lib\chavier\gui.py -> build\bdist.win-amd64\egg\chavier
copying build\lib\chavier\__init__.py -> build\bdist.win-amd64\egg\chavier
creating build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\bar.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\chart.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\color.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\line.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\pie.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\scatter.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\stackedbar.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\utils.py -> build\bdist.win-amd64\egg\pycha
copying build\lib\pycha\__init__.py -> build\bdist.win-amd64\egg\pycha
byte-compiling build\bdist.win-amd64\egg\chavier\app.py to app.pyc
byte-compiling build\bdist.win-amd64\egg\chavier\dialogs.py to dialogs.pyc
byte-compiling build\bdist.win-amd64\egg\chavier\gui.py to gui.pyc
byte-compiling build\bdist.win-amd64\egg\chavier\__init__.py to __init__.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\bar.py to bar.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\chart.py to chart.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\color.py to color.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\line.py to line.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\pie.py to pie.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\scatter.py to scatter.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\stackedbar.py to stackedbar.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\utils.py to utils.pyc
byte-compiling build\bdist.win-amd64\egg\pycha\__init__.py to __init__.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying pycha.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying pycha.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying pycha.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying pycha.egg-info\entry_points.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying pycha.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying pycha.egg-info\zip-safe -> build\bdist.win-amd64\egg\EGG-INFO
creating dist
creating 'dist\pycha-0.6.0-py2.7.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing pycha-0.6.0-py2.7.egg
Copying pycha-0.6.0-py2.7.egg to d:\tmp\dev_install_root\python27_x64\lib\site-packages
Adding pycha 0.6.0 to easy-install.pth file
Installing chavier-script.pyw script to D:\tmp\dev_install_root\Python27_x64\Scripts
Installing chavier.exe script to D:\tmp\dev_install_root\Python27_x64\Scripts
Installing chavier.exe.manifest script to D:\tmp\dev_install_root\Python27_x64\Scripts

Installed d:\tmp\dev_install_root\python27_x64\lib\site-packages\pycha-0.6.0-py2.7.egg
Processing dependencies for pycha==0.6.0
Finished processing dependencies for pycha==0.6.0

 

使用pycha

都安装好了,就可以去试试效果了。

参考:

http://packages.python.org/pycha/tutorial.html

去写代码。

后来还是从:

https://bitbucket.org/lgs/pycha/src/fa89153081d0/examples?at=default

找到:

https://bitbucket.org/lgs/pycha/src/fa89153081d0/examples/piechart.py?at=default

试了试代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【记录】折腾Python中的一个绘制图表的模块:pycha
http://www.crifan.com/python_making_charts_module_pycha

Author:     Crifan Li
Version:    2013-01-04
Contact:    admin at crifan dot com
"""

import cairo;
import sys;
import cairo;
import pycha.pie;
from lines import lines;

def pycharDemo(output):
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 800, 800)

    dataSet = [(line[0], [[0, line[1]]]) for line in lines]

    options = {
        'axis': {
            'x': {
                'ticks': [dict(v=i, label=d[0]) for i, d in enumerate(lines)],
            }
        },
        'legend': {
            'hide': True,
        },
        'title': 'Pie Chart',
    }
    chart = pycha.pie.PieChart(surface, options)

    chart.addDataset(dataSet)
    chart.render()

    surface.write_to_png(output)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        output = sys.argv[1]
    else:
        output = 'piechart.png'
    pycharDemo(output);

运行结果竟然又是缺少一个lines:

D:\tmp\tmp_dev_root\python\tutorial_summary\pycha_demo>pycha_demo.py
Traceback (most recent call last):
  File "D:\tmp\tmp_dev_root\python\tutorial_summary\pycha_demo\pycha_demo.py", line 17, in <module>
    from lines import lines;
ImportError: No module named lines

所以,还得再去安装lines:

【已解决】Python代码运行出错:ImportError: No module named lines

然后后来就是去把相关示例代码都下载下来了:

all demo files

此处,运行对应代码,会生成对应的piechart.png,效果和开始官网的例子,一模一样:

my generated piechart

再去试试其他的:

barchart.py会生成两个:

my vbarchart

my hbarchart

 

errorbarchart.py也是生成两个:

verrorbars

herrorbars

 

interval.py的效果:

interval

 

linechart.py的效果:

linechart

 

pychadownloads.py的效果:

pychadownloads

 

scatterchart.py的效果:

scatterchart

 

stackedbarchart.py效果:

hstackedbarchart

vstackedbarchart

 

svg.py会生成对应的testsvg.svg,效果是:

testsvg svg effect

 

test.py的效果,三个:

testpie

testline

testbar

 

总之,效果相当的不错。

 

总结

pycha,安装起来,稍微有点麻烦,但是效果那是相当地不错哇。



2 Thoughts on “【记录】折腾Python中的一个绘制图表的模块:pycha

  1. 已经按照提示安装pycha,可是运行您提供的example的代码还是提示from _cairo import *
    ImportError: DLL load failed: 找不到指定的模块。查看python官网,也不确定是不是cairo安装过程中缺少对应的dll文件,求解!

  2. 按照您提供的步骤,安装了

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量