Python: Use pycha to quickly draw common office charts (seven types of charts, including pie chart, vertical histogram, horizontal histogram, and scatter chart)

Source: Internet
Author: User
Tags cairo

This evening I learned how to use the pycha module to create various office charts. This is very simple. In this article, I have used seven charts.

This module and the API address are: Release.

I. Code:

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import Cairo import pycha. pieimport pycha. barimport pycha. scatterimport pycha. stackedbarimport pycha. line # Set the canvas def set_charvalue (): width, height = 600,600 surface = Cairo. imagesurface (Cairo. format_argb32, width, height) return surface # Pie image def draw_pie (surface, options, dataset): Chart = pycha. pie. piechart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ pie.png ') # vertical histogram def draw_vertical_bar (surface, options, dataset): Chart = pycha. bar. verticalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ vertical_bar.png ') # Vertical Horizontal histogram def draw_horizontal_bar (surface, options, dataset): Chart = pycha. bar. horizontalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ horizontal_bar.png ') # line chart def draw_line (surface, options, dataset): Chart = pycha. line. linechart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ line.png ') # Point Graph def draw_scatterplot (surface, options, dataset): Chart = pycha. scatter. scatterplotchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ scatterplotchart.png ') # Vertical Block image def draw_stackedverticalbarchar (surface, options, dataset): Chart = pycha. stackedbar. stackedverticalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ stackedverticalbarchart.png ') # horizontal block graph def draw_stackedhorizontalbarchart (surface, options, dataset): Chart = pycha. stackedbar. stackedhorizontalbarchart (surface, options) chart. adddataset (Dataset) chart. render () surface. write_to_png ('d: \ stackedhorizontalbarchart.png ') If _ name _ =' _ main _ ': ''' function: Use pycha to draw various charts. input: none output: None Author: Socrates blog: http://blog.csdn.net/dyx1024 Date: 2012-02-28 ''' # data source dataset = ('iphone ', (), (2, 2.5), ('htc ', (), ('hw ,), (2, 0.5), ('zte', (1.5), (,), (2 ))),) # image attribute definition Options = {'gend': {'hide ': false}, 'title': 'mobile phone sales distribution chart (by dyx1024)', 'titlecolor ': '# 0000ff', 'titlefont': 'font', 'background': {'chartcolor':' # ffff'}, 'axis ': {'labelcolor ': '# ff000000'},} surface = set_charvalue () # Call different functions to draw graphs of different shapes as needed # draw_pie (surface, options, dataset) # draw_vertical_bar (surface, options, dataset) # draw_horizontal_bar (surface, options, dataset) # draw_scatterplot (surface, options, dataset) # draw_stackedverticalbarchar (surface, options, dataset) # surface, options, dataset) draw_line (surface, options, dataset)

Ii. test:

1. Function draw_pie (surface, options, dataset ):

 

2. Function draw_vertical_bar (surface, options, dataset ):

3. Function draw_horizontal_bar (surface, options, dataset ):

4. Function draw_line (surface, options, dataset ):

5. Function draw_scatterplot (surface, options, dataset ):

6. Function draw_stackedverticalbarchar (surface, options, dataset ):

7. Function draw_stackedhorizontalbarchart (surface, options, dataset ):

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.