python怎么画图表_Python 使用pycha画图表

该博客介绍了如何利用Python的Pycha库创建饼图和直方图。首先,文章讲解了安装Cairo库的必要性,然后展示了如何设置画布和图像属性。接着,提供了两个函数分别用于绘制饼图和直方图,并给出了具体的代码示例。数据集被设置为服务器销售分布,最后展示了生成的饼图和直方图的图片。这为Python初学者提供了一个可视化数据的简单教程。
摘要由CSDN通过智能技术生成

事前准备:

下载并安装:

Cairo:

Pycha:

简单的程序示例如下(包括饼图和直方图):

#!/usr/bin/env python

# -*-coding:utf-8-*-

import pycha.pie

import pycha.bar

import pycha.stackedbar

import pycha.scatter

#设置画布

def set_surface():

width,height=700,700

return surface

#饼图

def draw_pie(surface,options,dataSet):

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

chart.addDataset(dataSet)

surface.write_to_png(‘D:\\Pie.png‘)

#垂直立方图

defdraw_vertical_bar(surface,options,dataSet):

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

chart.addDataset(dataSet)

chart.render()

surface.write_to_png(‘d:\\VerticalBar.png‘)

if __name__==‘__main__‘:

#设置数据

dataSet=(

(‘IBM‘,((1,3),(2,4),(3,6))),

(‘HP‘,((1,3.3),(2.1,4.3),(2,5))),

(‘DELL‘,((2,3.3),(3.1,3.3),(3,5))),

)

#设置图像属性

options={

‘legend‘:{‘hide‘:False},

‘title‘:‘服务器销售分布图(design byWoody)‘,

‘titleColor‘:‘#0000ff‘,

‘titleFont‘:‘字体‘,

‘background‘:{‘chartColor‘:‘#00fff0‘},

‘axis‘:{‘labelColor‘:‘#ff000‘},

}

surface=set_surface()

#调用不同的方法生成相应的销售图表

draw_pie(surface,options,dataSet)

draw_vertical_bar(surface,options,dataSet)

运行结果图如下:

Center

Center

Python isPython!

关于Cairo:

http://www.cairographics.org/

关于ImageSurface:

Creates an image surface of the specifiedformat and dimensions. Initially the surface contents are all 0. (Specifically,within each pixel, each color or alpha channel belonging to format will be 0.The contents of bits within a pixel, but not

belonging to the given format areundefined).

format :

format of pixels in the surface to create

width :

width of the surface, in pixels

height :

height of the surface, in pixels

Returns :

a pointer to the newly created surface. The caller owns the surface and should call cairo_surface_destroy() when

done with it. This function always returns a valid pointer, but it will return a pointer to a "nil" surface if an error such as out of memory occurs. You can use cairo_surface_status() to

check for this.

求解中…

原文地址:http://blog.csdn.net/woody891/article/details/37031103

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值