Node:Introduction, Next:Options, Previous:Copying, Up:Top
This document describes PyChart Version 1.30, a Python library designed for drawing professional-quality charts. It produces line plots, bar plots, range-fill plots, and pie charts in Postscript, PDF, PNG, or SVG. The following sections illustrate some uses of Pychart.
This example draws a simple line plot. Below is the source code needed to produce this chart.
../demos/linetest.py
# This line imports all the chart modules. from pychart import * theme.get_options() # We have 10 sample points total. The first value in each tuple is # the X value, and subsequent values are Y values for different lines. data = [(10, 20, 30), (20, 65, 33), (30, 55, 30), (40, 45, 51), (50, 25, 27), (60, 75, 30), (70, 80, 42), (80, 62, 32), (90, 42, 39), (100, 32, 39)] # The format attribute specifies the text to be drawn at each tick mark. # Here, texts are rotated -60 degrees ("/a-60"), left-aligned ("/hL"), # and numbers are printed as integers ("%d"). xaxis = axis.X(format="/a-60/hL%d", tic_interval = 20, label="Stuff") yaxis = axis.Y(tic_interval = 20, label="Value") # Define the drawing area. "y_range=(0,None)" tells that the Y minimum # is 0, but the Y maximum is to be computed automatically. Without # y_ranges, Pychart will pick the minimum Y value among the samples, # i.e., 20, as the base value of Y axis. ar = area.T(x_axis=xaxis, y_axis=yaxis, y_range=(0,None)) # The first plot extracts Y values from the 2nd column # ("ycol=1") of DATA ("data=data"). X values are takes from the first # column, which is the default. plot = line_plot.T(label="foo", data=data, ycol=1, tick_mark=tick_mark.star) plot2 = line_plot.T(label="bar", data=data, ycol=2, tick_mark=tick_mark.square) ar.add_plot(plot, plot2) # The call to ar.draw() usually comes at the end of a program. It # draws the axes, the plots, and the legend (if any). ar.draw()
To produce a Postscript chart, just feed the file to Python.
% python linetest.py >linetest.eps
Or, to produce a PDF chart, run python like below
1:
% python linetest.py --format=pdf >linetest.pdf
PyChart also supports PNG, SVG, and interactive X11 display. For more information about output control, Options.
Every pychart program starts with from pychart import *
to import
classes and objects provided by PyChart. Each chart is represented by
an area
object, which defines the size , the coordinate system
(linear, log, etc), and plots to be drawn in the chart. The final line
of the program should end with area.draw()
, which draws all the
components of the chart to the standard output.
Above example is a little more complicated chart. the below program generates this chart.
../demos/bartestv.py
from pychart import * theme.get_options() data = [(10, 20, 30, 5), (20, 65, 33, 5), (30, 55, 30, 5), (40, 45, 51, 7), (50, 25, 27, 3), (60, 75, 30, 5), (70, 80, 42, 5), (80, 62, 32, 5), (90, 42, 39, 5), (100, 32, 39, 4)] # The attribute y_coord_system="category" tells that the Y axis values # should be taken from samples, y_category_col'th column of # y_category_data. Thus, in this example, Y values will be # [40,50,60,70,80]. ar = area.T(y_coord = category_coord.T(data[3:8], 0), x_grid_style=line_style.gray50_dash1, x_grid_interval=20, x_range = (0,100), x_axis=axis.X(label="X label"), y_axis=axis.Y(label="Y label"), bg_style = fill_style.gray90, border_line_style = line_style.default, legend = legend.T(loc=(80,10))) # Below call sets the default attributes for all bar plots. chart_object.set_defaults(bar_plot.T, direction="horizontal", data=data) ar.add_plot(bar_plot.T(label="foo", cluster=(0,3))) ar.add_plot(bar_plot.T(label="bar", hcol=2, cluster=(1,3))) ar.add_plot(bar_plot.T(label="baz", hcol=3, cluster=(2,3))) ar.draw()
To handle command-line options such as --format=pdf
,
you need to put theme.get_options()
in the beginning of your file.
See Options.