Prerequisites:
Download and install:
Cairo:
Http://www.lfd.uci.edu /~ Gohlke/pythonlibs/bux9zozk/pycairo-1.10.0.win32-py2.7.exe
Pycha:
Https://bitbucket.org/lgs/pycha/get/e3e270a0e7ae.zip
A simple program example is as follows (including a pie chart and a histogram ):
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Import cairo [A1]
Import pycha. pie
Import pycha. bar
Import pycha. stackedbar
Import pycha. scatter
# Setting canvas
Def set_surface ():
Width, height = 700,700
Surface = cairo. ImageSurface (cairo. FORMAT_ARGB32, width, height) [A2]
Return surface
# Pie Chart
Def draw_pie (surface, options, dataSet ):
Chart = pycha. pie. PieChart (surface, options)
Chart. addDataset (dataSet)
Chart. render () [A3]
Surface. write_to_png ('d: \ Pie.png ')
# Vertical cube chart
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 __':
# Set Data
DataSet = (
('Ibm ', (1, 3), (2, 4), (3, 6 ))),
('Hp ', (1, 3.3), (2.1, 4.3 ))),
('Dell ', (2, 3.3), (3.1, 3.3 ))),
)
# Setting image attributes
Options = {
'Legend': {'hide ': False },
'Title': 'Design byWoody )',
'Titlecolor': '# 0000ff ',
'Titlefont': 'font ',
'Background': {'chartcolor': '#00fff0 '},
'Axis ': {'labelcolor':' # ff000 '},
}
Surface = set_surface ()
# Call different methods to generate corresponding sales charts
Draw_pie (surface, options, dataSet)
Draw_vertical_bar (surface, options, dataSet)
The running result is shown as follows:
Python isPython!
[A1]
About Cairo:
Http://www.cairographics.org/
[A2]
About 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 shocould 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. |
[A3]
Solving...