您的位置:首页 > 编程语言 > Python开发

关于迅速上手Pychart的简单介绍

2016-03-29 11:34 776 查看
博主在项目中使用python画图时,初碰Pychart,网上关于Pychart的使用说明较少,因此,碰到了几个新手容易出现的问题,现在把经验分享一下,好让这样的Pychart的菜鸟少走些弯路

步骤一:安装Pychart

官方下载地址:http://home.gna.org/pychart/

1.解压文件到你工作目录下,例如/Users/lmm/Documents/baidu/python

2.cd到解压后的文件夹目录下面,执行命令python setup.py install进行安装,上一步解压出来的文件夹内有setup.py文件

步骤二:使用

1.导入安装包,from pychart import *

2.使用示例:



#
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
#
# Pychart is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Pychart is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
from pychart import *
theme.get_options()
data = [["Jan", 10], ["Feb", 22], ["Mar", 30]]

ar = area.T(x_coord = category_coord.T(data, 0), y_range = (0, None),
x_axis = axis.X(label="Month"),
y_axis = axis.Y(label="Value"))
ar.add_plot(bar_plot.T(data = data, label = "Something"))
ar.draw()
文档地址:http://home.gna.org/pychart/doc/index.html

更多的示例:http://home.gna.org/pychart/examples/index.html(里面包含源码)

步骤三:生成图片

这边新手容易存在误区,发现运行python xxx.py时,没有图片生成,而是一些类似乱码的玩意儿,不用着急,Pychart不是这样使用的。下面是正确的运行方式:

1.例如需要生成pdf图片,python xxx.py --format=pdf >xxx.pdf(>右边的文件地址可以随意指定)

2.生成eps文件,python xxx.py >xxx.eps

然后去刚才填写生成文件的目录下就可以看到生成的文件啦,有没有很神奇。

FAQ:

Q:生成的PDF根本就没有图片,或者打不开

A:需要安装Ghostscript,地址http://www.ghostscript.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息