[Top] | [Contents] | [Index] | [ ? ] |
pychart Manual
yasushi@cs.washington.edu
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
pychart is a Python library for creating "professional" quality charts. It produces line plots, bar plots, range-fill plots, and pie charts in Postscript or PDF.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This example draws a simple line plot. Below is the source code used to produce this chart.
linetest.py:
# This line imports all the chart modules. from pychart import * # 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 speficies 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, legend=legend.T(), y_range=(0,None)) # The first plot extracts the X values from the 1st column ("xcol=0") # of DATA ("data=data"), and the Y values from the 2nd column # ("ycol=1") of DATA. plot = line_plot.T(label="foo", data=data, xcol=0, ycol=1, tick_mark=tick_mark.star) plot2 = line_plot.T(label="bar", data=data, xcol=0, 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 the chart, just feed the file to Python.
python linetest.py >linetest.eps |
Or, to produce a PDF chart, run python like below (see section 4. Options).
% setenv PYCHART_OPTIONS "output=pdf" % python linetest.py >linetest.pdf |
Every pychart program starts with from pychart import *
to import
all the objects and functions provided by pychart. Each chart is
represented by the area
object, which defines the size , the
coordinate system (linear, log, etc), and all the plots to be drawn in
the chart. The final line of the program should end with
area.draw()
that draws all the components of the chart to the
standard output.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Above example is a little more complicated chart. the below program generates this chart.
from pychart import *; 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_system = 'category', y_category_data = data[3:8], y_category_col = 0, x_range = (0,100), x_axis=axis.X(label="X label", tic_interval=20, grid_interval=20, grid_style=line_style.gray5dash1), y_axis=axis.Y(label="Y label"), legend = legend.T(), bg_style = fill_style.gray9, border_line_style = line_style.default) # 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), fill_style=fill_style.diag)) ar.add_plot(bar_plot.T(label="baz", hcol=3, cluster=(2,3), fill_style=fill_style.rdiag)) ar.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A chart comprises a set of components. Each component belongs to a certain class, from which an instance tailored for a particular chart is generated. The standard set of component classes follow:
area
class defines the size, the location, and the coordinate
system (linear, logarithmic, etc) of a chart (see section 9. Area). It also
contains axes, plots, and legends, as described below. At least one
Area needs to be created in any chart.
axis.X
and
axis.Y
, corresponding to horizontal and vertical axes.
plot
class actually draws a chart. Several subclasses of
plots are provided by pychart, including line plots
(see section 11. Line and scatter plots), range-fill plots (see section 12. Range plots), bar plots
(see section 13. Bar plots), and pie plots (see section 14. Pie plots). You can draw
multiple plots in a single chart, and most of the times you can even mix
different types of plots, e.g., line plots and bar plots.
legend
class is an optional rectangular box that describes
what each plot means (see section 15. Legends).
text_box
is an optional rectangular box that contains
arbitrary text. It can also contain arrows (see section 21. Annotation).
canvas
is a "virtual paper" that defines graph-drawing
primitives, such as lines, rectangles, and texts. Canvas is used by
other components in the graph, and usually it is not manipulated
directly by users. It's handy, however, if you want to draw a
line/circle/text/etc, directly on the Postscript or PDF file
(see section 24.1 Drawing arbitrary objects).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Environment variable `PYCHART_OPTIONS' is used to control the behavior of pychart. The variable is a sequence of `var=val' separated by commas (no space between commas allowed). Below is an example:
PYCHART_OPTIONS="output=pdf,font-family=Times" |
pychart, by default, produces data in encapsulated Postscript to the
standard output. The output format is controlled using `output'
option. Its value should be one of ps
(for Postscript output),
pdf
(for PDF compressed output), or pdf-nocompress
(for
PDF non-compressed output. No space after comma).
Set the default font family and size to be used when drawing text. See section 20. Texts.
Set the default line width, in points.
Set the scaling factor (i.e., `scaling.scale_factor'). See section 6. Unit of length.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Each component in a chart (see section 3. Anatomy of a chart) is described by a collection
of attributes. For example, Axis (see section 10. Axes) includes the
integer tic_len
attribute that defines the length of "tick" lines
drawn perpendicular to the axis. It also has the integer
tic_interval
attribute that defines the separation between tick
lines (for the full list of Axis's attributes, See section 10. Axes).
Attributes are usually specified via named arguments while creating components:
ax = axis.X(tic_len = 3, tic_interval = 50) |
Attributes can also be changed after a component is created, but before
area.draw()
, the main drawing function (see section 9. Area), is called.
The below example has the same effect as the above.
ax = axis.X() ax.tic_len = 3 ax.tic_interval = 50 |
Each attribute has a default value that supposedly gives you a standard
look to the chart. You can change the default values by using the
chart_object
module. For example, the below example has the same
effect as above, except that all the X axes that may be created
afterwards have the new default values.
chart_object.set_defaults(axis.X, tic_len=3, tic_interval=50) ax = axis.X() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Lengths are measured by "Postscript points", which coincides with TeX points. One point is equal to 1/72 inch.
scaling.scale_factor = 3
, then
everything is drawn three times larger.
ar = area.T(loc=(50, 50), size=(100, 100), xrange=(0,200), yrange=(0, 1000)) px = ar.x_pos(50) py = ar.y_pos(100) |
In the above example, the chart is drawn on the area defined by
rectangle (50, 50) - (150, 150). The point (px, py
) will
be at (75, 60), which is the screen location at which the point
(50, 100) would be drawn (i.e., 50 + 100 * 50/200 = 75,
50 + 100 * 100 / 1000 = 60).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In pychart, X axis grows to the right, and Y axis grows up (same as Postscript, but different from X and Windows).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The basic function of pychart is to draw sample points in
a variety of ways. Sample points are simply a sequence of sequences,
where "sequence" is a Python term that stands for either a tuple
(comma-separated numbers enclosed in parenthesis) or a list
(comma-separated numbers enclosed in square brackets). Data are given to
plots through the "data
" attribute:
data = [(10,20), (11,38), (12,29)] l = line_plot.T(data=data, xcol=0, ycol=1) |
In the above example, three sample points will be drawn along with line
segments that connect the points: (10, 20) - (11, 38) - (12, 29).
Attribute xcol
tells the plot where to find the X values (0th
column of each sample in data
), and ycol
similarly tell
she plot where to find the Y values (next column of each sample in
data
).
Module chart_data
is provided to create data in various ways.
This function reads comma-separated samples from FILE. Empty lines
and lines beginning with "`#'" are ignored. DELIM specifies
how values in each line are separated. If it does not contain the letter
"%
", then DELIM is simply used to split each line into
values. Otherwise, this function acts like scanf
in C:
chart_data.read_csv("file", "%d,%s:%d") |
This function currently supports only three conversion specifiers: "d"(int), "f"(double), and "s"(string).
read_csv
, but this function reads
from an open file handle FP.
fp = open("foo", "r") data = chart_data.fread_csv(fp, ",") |
read_csv
, but this function reads data from the list
of lines.
fp = open("foo", "r") data = chart_data.read_str(",", fp.readlines()) |
Create sample points from function F, which must be a
single-parameter function that returns a number (e.g., math.sin
).
FROM and TO are the first and last X values. STEP
specfies the sampling interval.
sin_samples = chart_data.func(math.sin, 0, math.pi*4, 0.1) |
F must be a single-argument function that takes a sequence (i.e.,
a sample point) and returns a boolean. Filter
calls F on
each element in data and returns a list comprising elements for
which which F returns true.
data = [[1,5], [2,10], [3,13], [4,16]] data2 = chart_data.filter(lambda x: x[1] % 2 == 0, data) # data2 will become [[2,10], [4,16]]. |
Extract the rows specified in the argument list and returns a new data.
data2 = chart_data.extract_rows([[10,20], [30,40], [50,60]], 1, 2) # data2 will become [[30,40],[50,60]]. |
Extract the columns specified in the argument list and returns a new data. For example,
data2 = chart_data.extract_columns([[10,20], [30,40], [50,60]], 0) # data2 will become [[10],[30],[50]]. |
Apply the function F on each element in DATA, and return the list consisting of the return values from F.
data = [[10,20], [30,40], [50,60]] data2 = chart_data.transform(lambda x: [x[0], x[1]+1], data) # data2 will become [[10, 21], [30, 41], [50, 61]]. |
Compute the moving average of YCOL'th column of each sample point in DATA. In particular, for each element I in DATA, this function extracts up to WIDTH*2+1 elements, consisting of I itself, WIDTH elements before I, and WIDTH elements after I. It then computes the mean of the YCOL'th column of these elements, and it composes a two-element sample consisting of XCOL'th element and the mean.
data = [[10,20], [20,30], [30,50], [40,70], [50,5]] data2 = chart_data.moving_average(data, 0, 1, 1) |
In the above example, data2
will be computed as:
[(10, (20+30)/2), (20, (20+30+50)/3), (30, (30+50+70)/3), (40, (50+70+5)/3), (50, (70+5)/2)] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Class area.T
defines the location and the size of a chart. It
also defines coordinate systems (linear, log, or enumeration) of the X
and Y axes.
Attribute | Type | Default | Description |
bg_style |
fill_style.T | None | Background of the chart. See section 19. Fill styles. |
border_line_style |
line_style.T | None | Line style of the frame that surrounds the chart. See section 16. Line styles. |
legend |
legend.T | None | The legend of the chart. See section 15. Legends. |
loc |
pair of int | (0, 0) | Bottom-left corner of the chart. |
size |
pair of int | (120, 110) | Size of the chart drawing area, excluding axis labels, legends, tick marks, etc. |
x_axis |
axis.X | None | The X axis. See section 10. Axes.. |
x_category_col |
int | 0 | Selects values from 'x_category_data'. |
x_category_data |
list | None | Meaningful only when 'x_coord_system' is 'category'. 'x_category_col'th column of each tuple in x_category_data defines the set of X values. |
x_coord_system |
'linear'/'log'/'category' | linear | Either 'linear', 'log' or 'category'. Linear and log makes x coordinate scaled linearly or logarithmically. Category enumerates the X values through x_category_data and x_category_col attributes. |
x_grid_over_plot |
int | 0 | If true, grid lines are drawn over plots. |
x_log_base |
int | 10 | |
x_range |
pair of int | None | The minimum and maximum X values. |
y_axis |
axis.Y | None | The Y axis. See section 10. Axes.. |
y_category_col |
int | 1 | See x_category_col. |
y_category_data |
list | None | See x_category_data. |
y_coord_system |
'linear'/'log'/'category' | linear | Set the Y coordinate scaling. See also x_coord_system. |
y_grid_over_plot |
int | 0 | See x_grid_over_plot. |
y_log_base |
int | 10 | |
y_range |
pair of int | None | The minimum and maximum Y values. |
y_zap |
pair of int | None | Compress the section. |
y_zap_entire_area |
int | None | If 1, then the entire area is horizontally `zapped'. |
Below are the methods defined in area.T objects:
plot
must be a plot. See section 11. Line and scatter plots, section 13. Bar plots,
section 14. Pie plots, section 12. Range plots.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Module Axis defines the look of an axis and grid lines. Two classes, `axis.X' and `axis.Y', are provided by this module.
Attribute | Type | Default | Description |
first_tic_value |
number | None | The location of the first tick mark. Defaults to the minimum value of the axis. |
format |
printf format string | %s | The format string for tick labels. It can be a `printf' style format string, or a single-parameter function that returns a string. See section 20. Texts.. |
grid_interval |
A number of a function | 0 | When the value is a number, it specifies the interval with which grid lines are drawn. Otherwise, the value must be a function. It must take no argument and return the list of numbers, which specifies the X or Y points where grid lines are drawn. |
grid_style |
line_style.T | None | The style of grid lines. See section 16. Line styles. |
label |
string | axis label | The description of the axis. See section 20. Texts.. |
label_offset |
pair of int | (None, None) | The location where the axis label is drawn. Relative to the left-bottom corner of the axis. |
line_style |
line_style.T | black | The style of tick lines. See section 16. Line styles. |
minor_tic_interval |
A number of a function | None | When the value is a number, it specifies the interval with which minor tick marks are drawn. Otherwise, the value must be a function. It must take no argument and return the list of numbers, which specifies the X or Y points where minor tick marks are drawn. |
minor_tic_len |
number | 3 | The length of minor tick marks. |
tic_interval |
A number of a function | None | When the value is a number, it specifies the interval with which tick marks are drawn. Otherwise, the value must be a function. It must take no argument and return the list of numbers, which specifies the X or Y points where tick marks are drawn. |
tic_label_offset |
pair of int | (0, 0) | The location where the tick labels is drawn. Relative to the tip of the tick mark. |
tic_len |
number | 6 | The length of tick lines |
Attribute | Type | Default | Description |
first_tic_value |
number | None | The location of the first tick mark. Defaults to the minimum value of the axis. |
format |
printf format string | %s | The format string for tick labels. It can be a `printf' style format string, or a single-parameter function that returns a string. See section 20. Texts.. |
grid_interval |
A number of a function | 0 | When the value is a number, it specifies the interval with which grid lines are drawn. Otherwise, the value must be a function. It must take no argument and return the list of numbers, which specifies the X or Y points where grid lines are drawn. |
grid_style |
line_style.T | gray7dash3 | See section 16. Line styles. |
label |
string | axis label | The description of the axis. See section 20. Texts.. |
label_offset |
pair of int | (None, None) | The location where the axis label is drawn. Relative to the left-bottom corner of the axis. |
line_style |
line_style.T | black | The style of tick lines. See section 16. Line styles. |
minor_tic_interval |
A number of a function | None | When the value is a number, it specifies the interval with which minor tick marks are drawn. Otherwise, the value must be a function. It must take no argument and return the list of numbers, which specifies the X or Y points where minor tick marks are drawn. |
minor_tic_len |
number | 3 | The length of minor tick marks. |
tic_interval |
A number of a function | None | When the value is a number, it specifies the interval with which tick marks are drawn. Otherwise, the value must be a function. It must take no argument and return the list of numbers, which specifies the X or Y points where tick marks are drawn. |
tic_label_offset |
pair of int | (0, 0) | The location where the tick labels is drawn. Relative to the tip of the tick mark. |
tic_len |
number | 6 | The length of tick lines |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
data |
any | None | Specifies the data points. See section 8. Input data. |
data_label_format |
printf format string | None | The format string for label printed besides each sample point. It can be a `printf' style format string, or a two-parameter function that takes the (x, y) values and returns a string. See section 20. Texts.. |
data_label_offset |
pair of int | (0, 5) | Location of the data label relative to the sample point. |
error_bar |
error_bar.Base | None | The style of the error bar. See section 23. Error Bars. |
label |
string | ??? | The label to be displayed in the legend. See section 15. Legends., See section 20. Texts. |
line_style |
line_style.T | <function <lambda> at 0x8223804> | The style of the line. See section 16. Line styles. |
q_max_col |
int | None | See section 23. Error Bars. |
q_min_col |
int | None | See section 23. Error Bars. |
tick_mark |
tick_mark.Base | None | Tick marks to be displayed at each sample point. |
xcol |
int | 0 | The X values of sample points are extracted from XCOL'th column of data. See section 8. Input data. |
y_max_col |
int | None | The height of the errorbar is extracted from this column in data. See section 23. Error Bars. |
y_min_col |
int | 2 | The depth of the errorbar is extracted from this column in data. See section 23. Error Bars. |
ycol |
int | 1 | The X values of sample points are extracted from YCOL'th column of data. See section 8. Input data. |
Sample line plot
Below is the source code that produces Figure \ref{fig:linetest3}.
from pychart import * data = chart_data.read_csv("lines.csv") xaxis=axis.X(label="X", tic_interval=10) yaxis=axis.Y(label="Y", tic_interval=10) ar = area.T(x_range=(0,100), y_range=(0,100), x_axis=xaxis, y_axis=yaxis) eb = error_bar.error_bar2(tic_len=5, hline_style=line_style.gray5) ar.add_plot(line_plot.T(label="foo", data=data, error_bar=eb, y_min_col=3), line_plot.T(label="bar", data=data, ycol=2, error_bar=eb, y_min_col=3)) ar.draw() tb = text_box.T(loc=(40, 130), text="This is\nimportant!", line_style=None) tb.add_arrow((ar.x_pos(data[6][0]), ar.y_pos(data[6][1])), "cb") tb.draw() ar = area.T(loc=(200, 0), x_range=(0,100), y_range=(0,100), x_axis=xaxis, y_axis=yaxis, legend=legend.T()) ar.add_plot(line_plot.T(label="foo", data=data, data_label_format="/8{}%d"), line_plot.T(label="bar", data=data, ycol=2)) ar.draw() |
Scatter plots can be created by setting the lineStyle
attribute
to None
. See the next example.
Sample scatter plot
Below is the source code that produces Figure \ref{fig:scattertest}.
from pychart import * import whrandom def randomdata(): data = [] for i in range(0, 30): data.append((whrandom.random() * 1000, whrandom.random() * 1000)) return data font.default_size = 9 chart_object.set_defaults(line_plot.T, line_style=None) tick1 = tick_mark.Circle(size=2) tick2 = tick_mark.Circle(size=2, fill_style=fill_style.black) xaxis = axis.X(label="foo", format="/a-60{}%d") yaxis = axis.Y(label="bar") ar = area.T(x_axis=xaxis, y_axis=yaxis, legend = legend.T(loc=(350, 50)), loc = (0, 0)) ar.add_plot(line_plot.T(label="plot1", data=randomdata(), tick_mark=tick1)) ar.add_plot(line_plot.T(label="plot2", data=randomdata(), tick_mark=tick2)) ar.draw() xaxis = axis.X(label="foo", format="/a-30{}%d", grid_interval=10, grid_style=line_style.gray7dash3) yaxis = axis.Y(label="bar") ar = area.T(x_axis=xaxis, y_axis=yaxis, x_coord_system='log', y_coord_system='log', legend=None, loc = (200, 0)) ar.add_plot(line_plot.T(label="plot1", data=randomdata(), tick_mark=tick1)) ar.add_plot(line_plot.T(label="plot2", data=randomdata(), tick_mark=tick2)) ar.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
data |
any | None | Specifies the data points. See section 8. Input data. |
fill_style |
fill_style.T | default | See section 19. Fill styles. |
label |
string | ??? | The label to be displayed in the legend. See section 15. Legends., See section 20. Texts. |
line_style |
line_style.T | black | The style of the boundary line. See section 16. Line styles. |
max_col |
int | 2 | The upper bound of the sweep is extracted from this column of data. |
min_col |
int | 1 | The lower bound of the sweep is extracted from this column of data. |
xcol |
int | 0 | The X values of sample points are extracted from XCOL'th column of data. See section 8. Input data. |
Sample range plot
Below is the source code that produces Figure \ref{fig:rangetest}.
from pychart import * data = [ (0, 10, 30, 40, 60), (10, 20, 40, 50, 55), (20, 10, 35, 38, 43), (30, 8, 30, 35, 39), (40, 8, 20, 28, 39) ] ar = area.T(x_axis = axis.X(label="X axis"), y_axis = axis.Y(label="Y axis", grid_interval = 10, grid_style = line_style.white), y_grid_over_plot=1, legend = legend.T()) ar.add_plot(range_plot.T(label="foo", data=data, fill_style = fill_style.gray9)) ar.add_plot(range_plot.T(label="bar", data=data, min_col=2, max_col=3, fill_style = fill_style.white)) ar.add_plot(range_plot.T(label="baz", data=data, min_col=3, max_col=4, fill_style = fill_style.gray5)) ar.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
bcol |
int | 0 | Base (i.e., X when DIRECTION is vertical, Y otherwise) value of each bar is extracted this column in data. |
cluster |
tuple | (0, 1) | foo |
cluster_sep |
number | 0 | Separation between clustered boxes |
data |
any | None | Specifies the data points. See section 8. Input data. |
data_label_format |
printf format string | None | The format string for label printed besides each bar. It can be a `printf' style format string, or a two-parameter function that takes (x,y) values and returns a string. See section 20. Texts.. |
data_label_offset |
pair of int | (0, 5) | Location of data label relative to the sample point. |
direction |
string | vertical | The direction the bars grows. Either 'horizontal' or 'vertical'. |
error_bar |
error_bar.Base | None | The style of the error bar. See section 23. Error Bars. |
fill_style |
fill_style.T | <function <lambda> at 0x822ae64> | Fill style of each box. See section 19. Fill styles. |
hcol |
int | 1 | Height of each bar is extracted this column in data. |
label |
string | ??? | The label to be displayed in the legend. See section 15. Legends., See section 20. Texts. |
line_style |
line_style.T | black | The style of the outer frame of each box. See section 16. Line styles. |
q_max_col |
int | None | |
q_min_col |
int | None | |
stack_on |
any | None | Each bar of this plot is stacked on top of the plot specified by this value. |
width |
number | 5 | Width of each box. |
y_max_col |
int | None | The depth of the errorbar is extracted from this column in data. |
y_min_col |
int | 2 | The depth of the errorbar is extracted from this column in data. |
Sample bar charts
Below is the source code that produces Figure \ref{fig:bartest}.
from pychart import * data = [(10, 20, 30, 5), (20, 65, 33, 5), (30, 55, 30, 5), (40, 45, 51, 7), (50, 25, 27, 3)] chart_object.set_defaults(area.T, size = (150, 120), y_range = (0, None), x_coord_system = 'category', x_category_data = data, x_category_col = 0) chart_object.set_defaults(bar_plot.T, data = data) # Draw the 1st graph. The Y upper bound is calculated automatically. ar = area.T(legend = legend.T(), x_axis=axis.X(label="X label", format="/a-30{}%d"), y_axis=axis.Y(label="Y label", tic_interval=10)) ar.add_plot(bar_plot.T(label="foo", cluster=(0, 3)), bar_plot.T(label="bar", hcol=2, cluster=(1, 3), fill_style=fill_style.diag), bar_plot.T(label="baz", hcol=3, cluster=(2, 3), fill_style=fill_style.rdiag)) ar.draw() ar = area.T(legend = legend.T(), loc=(250,0), x_axis=axis.X(label="X label", format="/a-30{}%d"), y_axis=axis.Y(label="Y label", tic_interval=10)) plot1=bar_plot.T(label="foo") plot2=bar_plot.T(label="bar", hcol=2, stack_on = plot1, fill_style=fill_style.diag) plot3=bar_plot.T(label="baz", hcol=3, stack_on = plot2, fill_style=fill_style.rdiag) ar.add_plot(plot1, plot2, plot3) ar.draw() |
Vertical bar chart
Below is the source code that produces Figure \ref{fig:bartestv}.
from pychart import *; 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_system = 'category', y_category_data = data[3:8], y_category_col = 0, x_range = (0,100), x_axis=axis.X(label="X label", tic_interval=20, grid_interval=20, grid_style=line_style.gray5dash1), y_axis=axis.Y(label="Y label"), legend = legend.T(), bg_style = fill_style.gray9, border_line_style = line_style.default) # 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), fill_style=fill_style.diag)) ar.add_plot(bar_plot.T(label="baz", hcol=3, cluster=(2,3), fill_style=fill_style.rdiag)) ar.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
arc_offsets |
list | None | |
center |
pair of int | None | |
data |
any | None | |
data_col |
int | 1 | |
fill_styles |
list | ['black', 'gray7', 'diag', 'gray3', 'rdiag', 'gray1', 'diag2', 'white', 'rdiag2', 'vert', 'diag3', 'gray5', 'horiz', 'gray9', 'rdiag3', 'wave', 'vwave', 'stitch', 'lines'] | |
label_fill_style |
fill_style.T | default | See section 19. Fill styles. |
label_line_style |
line_style.T | None | See section 16. Line styles. |
label_offset |
number | None | |
lable_col |
int | 0 | |
line_style |
line_style.T | black | See section 16. Line styles. |
radius |
number | None | |
shadow |
<function ShadowType at 0x814f20c> | None | |
start_angle |
number | 90 |
Sample pie chart
Below is the source code that produces Figure \ref{fig:pietest}.
from pychart import * import sys data = [("foo", 10),("bar", 20), ("baz", 30), ("ao", 40)] ar = area.T(size=(150,150), legend=legend.T()) plot = pie_plot.T(data=data, arc_offsets=[0,10,0,10], shadow = (2, -2, fill_style.gray5)) ar.add_plot(plot) ar.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
bottom_fudge |
number | 3 | Amount of space below the last line. |
frame_fill_style |
fill_style.T | white | See section 19. Fill styles. |
frame_line_style |
line_style.T | black | See section 16. Line styles. |
inter_row_sep |
number | 0 | Space between each entry in the legend. |
left_fudge |
number | 5 | Amount of space left of the legend. |
loc |
pair of int | None | Bottom-left corner of the legend. |
right_fudge |
number | 5 | Amount of space right of the legend. |
shadow |
<function ShadowType at 0x814f20c> | None | |
top_fudge |
number | 0 | Amount of space above the first line. |
The default location of a legend is the bottom-right end of the chart.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
cap_style |
int | 0 | Line cap style. 0: butt cap, 1: round cap, 2: projecting square cap. See also Postscript/PDF reference. |
color |
color.T | default | See section 18. Colors. |
dash |
tuple | None | The 2N'th value specifies the length of the line, and 2N+1'th value specfies the length of the blank. None draws a solid line. |
join_style |
int | 0 | Join style. 0: Miter join, 1: round join, 2: bevel join |
width |
number | default_line_width | Width of the line, in points. |
Standard line styles. These styles are referred to by names
line_style.
name, where name is the labels below them.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Tick marks are used in conjunction with line plots (see section 11. Line and scatter plots) to show where sample points are. pychart defines several standard tick marks shown below.
Attribute | Type | Default | Description |
fill_style |
fill_style.T | white | See section 19. Fill styles. |
line_style |
line_style.T | black | See section 16. Line styles. |
size |
number | 5 | Size of the tick mark. |
Several tick mark classes, which are subtypes of tick_mark.Base, are provided by pychart. They include: tick_mark.Circle, tick_mark.Square, tick_mark.Triangle, tick_mark.DownTriangle, tick_mark.X, TIckMark.Plus, tick_mark.Diamond, tick_mark.Star, tick_mark.Null.
Specific instance of tick mark classes are also defined for your
convenience. Below is the list of such standard tick marks. They are
refered to by as "tick_mark.
name", where name is the
labels below them.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You can use all the colors defined in X rgb.txt (usually at
`/usr/X11R6/lib/X11/rgb.txt'). The below shows some of the standard colors
defined in the module. They are refereed to by the names
color.
name (e.g., color.gray1
).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The class fill_style.T
determins the color and the pattern of the
background of a region.
Attribute | Type | Default | Description |
bgcolor |
color.T | white | The background color. See section 18. Colors. |
line_interval |
number | 3 | The interval between successive stitch lines. |
line_style |
line_style.T | black | The style of the line. See section 16. Line styles. |
The actual background patterns are offered by subclasses of
fill_style.T
:
This class just fills the region in the specified color. The attributes `line_style' and `line_interval' are ignored.
The below picture shows the standard set of fill styles. They are named
like fill_style.
name, where name are the labels shown
in the picture.
Just for your information, the "rdiag3" style shown in the picture can be created manually by the following code:
rdiag3 = fill_style.Rdiag(line_style=line_style.T(width=3, color=color.gray5), line_interval=6) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Text strings may contain escape characters that control the appearance of the text. See section 21. Annotation for the example of an use of escape sequences.
Sequence | Meaning | ||
/a dd |
Specifies the angle the text is drawn.
The parameter dd is a value between -360 to 360.
The value 0 means left-to-right text, 90 means bottom-to-up, etc.
If you want to print numbers right after the angle specification,
put {} between the angle and the number. For example,
the below displays "100" at 60-degree angle.
| ||
/h A |
Specifies horizontal alignment of the text. A is one of "L" (left alignment), "R" (right alignment), "C" (center alignment). | ||
/v A |
Specifies vertical alignment of the text. A is one of "B" (bottom), "T" (top), "M" (middle). | ||
/T | Switch to Times-Roman font family. | ||
/H | Switch to Helvetica font family. | ||
/C | Switch to Courier font family. | ||
/B | Switch to Bookman-Demi font family. | ||
/A | Switch to AvantGarde-Book font family. | ||
/P | Switch to Palatino font family. | ||
/S | Switch to Symbol font family. | ||
/b | Switch to bold typeface. | ||
/i | Switch to italic typeface. | ||
/o | Switch to oblique typeface. | ||
/ dd | Set the font size to dd points. |
"/20{}2001 space odyssey!" |
Restriction: `/h', `/v', and `/a' commands must appear at the beginning of a string.
The font module defines several procedures, described below, for manipulating fonts.
from pychart import * x = 100 y = 500 def show_text(str): global x, y canvas.show(x, y, str) y = y - 20 show_text("/20{}/HHelvetica") show_text("/20{}/CCourier") show_text("/20{}/NHelvetica-Narrow") show_text("/20{}/PPalatino-Roman") show_text("/20{}/BBookman-Demi") show_text("/20{}/AAvantgarde") show_text("/20{}/T/iTimes-Roman") show_text("/20{}/SSymbol") |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
arrows |
list | new_list | List of arrows. Not to be touched by the application directly |
bottom_fudge |
number | 5 | Space below the last line |
fill_style |
fill_style.T | white | Box fill style. See section 19. Fill styles. |
left_fudge |
number | 5 | Space left of the box |
line_style |
line_style.T | black | Frame box style. See section 16. Line styles. |
loc |
tuple | (0, 0) | Location of the text box. |
radius |
number | 0 | Radius of the four corners of the rectangle. |
right_fudge |
number | 5 | Space right of the box |
text |
string | ??? | Text body. See section 20. Texts. |
top_fudge |
number | 0 | Space above the first line |
In addition to the above attributes, it provides the following methods.
Adds a straight arrow to the text box that points to `tip', which is a tuple of integers. `Tail' is a string consisting of the following letters: 'l', 'c', or 'r' means to start the arrow from the left, center, or right of the text box, respectively. 't', 'm', or 'b' means to start the arrow from the top, middle or bottom of the text box. For example, when `tail = 'tc'' then arrow is drawn from top-center point of the text box. `arrow' specifies the style of the arrow. See section 22. Arrows.
from pychart import * a1 = text_box.T(loc=(100,100), text="Without frame") a1.add_arrow((50, 100)) a1.add_arrow((180, 100)) a1.draw() a1 = text_box.T(loc=(100,130), text="/hCMulti\n/bLine") a1.add_arrow((50, 120)) a1.add_arrow((180, 100)) a1.draw() a1 = text_box.T(loc=(100,160), text="Fat arrow", line_style=None) a1.add_arrow((180, 140), tail='rm', arrow = arrow.fat1) a1.draw() a1 = text_box.T(loc=(180, 100), text="/a90Funny background", fill_style = fill_style.gray7) a1.draw() a1 = text_box.T(loc=(180, 140), text="/hL/20Big/oText\n/24/bHuge/oText", fill_style = None) a1.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
head_color |
color.T | default | color of the arrow head. See section 18. Colors. |
head_len |
number | 8 | Length of the arrow head. |
head_style |
int | 1 | 0 draws a triangular arrow head. 1 draws a swallow-tail arrow head |
line_style |
line_style.T | black | Line style. See section 16. Line styles. |
thickness |
number | 4 | Length of the base of the arrow head. |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
line_style |
line_style.T | black | See section 16. Line styles. See section 16. Line styles. |
tic_len |
number | 10 | Length of the horizontal bars |
Attribute | Type | Default | Description |
hline_style |
line_style.T | black | The style of the horizontal bars. See section 16. Line styles.. See section 16. Line styles. |
tic_len |
number | 10 | The length of the horizontal bars |
vline_style |
line_style.T | None | The style of the vertical bar. See section 16. Line styles. |
Attribute | Type | Default | Description |
line_style |
line_style.T | black | See section 16. Line styles. |
Attribute | Type | Default | Description |
boxWidth |
number | 4 | |
fill_style |
fill_style.T | gray7 | See section 19. Fill styles. |
line_style |
line_style.T | black | See section 16. Line styles. |
tic_len |
number | 4 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
24.1 Drawing arbitrary objects 24.2 Clipping 24.3 Changing the output
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The `canvas' object, used internally by pychart, can be invoked manually if you desire so.
# draw a line from (10,20) to (100, 200) canvas.line(line_style.blackdash1, 10, 20, 100, 200) |
LINESTYLE can be None, in which case no perimeter lines are drawn. FILLSTYLE can also be None, in which the internal of the polygon are left undrawn.
canvas.polygon(line_style.default, fill_style.default, [(10, 20), (100, 200), (300,300)]) |
Parameter shadow, if none, draws a drop-shadow. It should be a tuple of three values, (x_off, y_off, fill).
canvas.rectangle(line_style.default, fill_style.default, 10, 20, 300, 300) |
The start and end angles are specified in degrees; i.e., between 0 and 360. Parameter shadow, if none, draws a drop-shadow. It should be a tuple of three values, (x_off, y_off, fill). .
Parameter shadow, if none, draws a drop-shadow. It should be a tuple of three values, (x_off, y_off, fill). .
TEXT supports font manipulation, as defined in See section 20. Texts.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A rectangle, polygon, or ellipsis region can be defined for clipping.
Any drawing commands (see section 24.1 Drawing arbitrary objects) issued afterwards are confined in
the region. You can even nest multiple clipping regions, in which case,
drawings will be clipped to the intersection of the regions.
canvas.endclip()
ends the clipping. Clipping and endclip()
must nest properly.
from pychart import * data = [(10, 20), (20, 65), (30, 55), (40, 45)] # tic_angle is the angle X values are displayed below the axis. xaxis = axis.X(label="Stuff") yaxis = axis.Y(label="Value") ar = area.T(x_axis=xaxis, y_axis=yaxis) plot = line_plot.T(label="foo", data=data, xcol=0, ycol=1, tick_mark=tick_mark.star) ar.add_plot(plot) canvas.ellipsis(line_style.T(width=1.5,dash=(4,4)), None, 30, 20, 80, 0.8) canvas.clip_ellipsis(30, 20, 80, 0.8) ar.draw() canvas.endclip() |
This function starts clipping on the rectangular region.
canvas.clip(x,y,x2,y2) draw something ... canvas.endclip() |
canvas.clip_ellipsis(x,y,radius,ratio) draw something ... canvas.endclip() |
canvas.clip_polygon([(x1,y1),(x2,y2),...,(xn,yn)]) draw something ... canvas.endclip() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To change the output stream to something other than stdout, do like below:
fp = open(...) canvas.newcanvas(fp) |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Jump to: | A B C D E F G H I J L M P Q R S T U V W X Y |
---|
Jump to: | A B C D E F G H I J L M P Q R S T U V W X Y |
---|
[Top] | [Contents] | [Index] | [ ? ] |
[Top] | [Contents] | [Index] | [ ? ] |
1. Introduction
2. Examples
3. Anatomy of a chart
4. Options
5. Attributes
6. Unit of length
7. Coordinate system
8. Input data
9. Area
10. Axes
11. Line and scatter plots
12. Range plots
13. Bar plots
14. Pie plots
15. Legends
16. Line styles
17. Tick marks
18. Colors
19. Fill styles
20. Texts
21. Annotation
22. Arrows
23. Error Bars
24. Tricks
Index
[Top] | [Contents] | [Index] | [ ? ] |
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ < ] | Back | previous section in reading order | 1.2.2 |
[ > ] | Forward | next section in reading order | 1.2.4 |
[ << ] | FastBack | previous or up-and-previous section | 1.1 |
[ Up ] | Up | up section | 1.2 |
[ >> ] | FastForward | next or up-and-next section | 1.3 |
[Top] | Top | cover (top) of document | |
[Contents] | Contents | table of contents | |
[Index] | Index | concept index | |
[ ? ] | About | this page |