[Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ 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 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 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. PyChart 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))) ar.add_plot(bar_plot.T(label="baz", hcol=3, cluster=(2,3))) 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 8. Area). It also
contains axes, plots, and legends, as described below. At least one
Area needs to be created in any chart.
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 10. Line and Scatter Plots), range-fill plots (see section 11. Range Plots), bar plots
(see section 12. Bar Plots), and pie plots (see section 13. 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 14. Legends).
text_box
is an optional rectangular box that contains
arbitrary text. It can also contain arrows (see section 20. 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 on Canvas).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An environment variable `PYCHART_OPTIONS' is used to control the behavior of PyChart{}. The value of this variable is a sequence of `var=val' separated by commas. No space between commas is allowed. Below is an example, which tells PyChart{} to direct output to file `foo.pdf' and use Times-Roman as the default font.
PYCHART_OPTIONS="output=foo.pdf,font-family=Times" |
output=
filename
Specifies the output file name. Without this option, PyChart{} sends code to the standard output.
format=
format
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).
font-family=
name
font-size=
size
Set the default font family and size to be used when drawing text. See section 19. Texts.
line-width=
x
Set the default line width, in points.
scale=
n
Set the scaling factor (i.e., `scaling.scale_factor'). The default is 1.0. See section 6. Coordinate System.
[ < ] | [ > ] | [ << ] | [ 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 9. Axes) includes an
integer tic_len
attribute that defines the length of "tick" lines
drawn perpendicular to the axis. It also has an integer
tic_interval
attribute that defines the separation between tick
lines. (For the full list of Axis's attributes, See section 9. 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 8. 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 through
chart_object
module. For example, the below example has the same
effect as the above, except that all X axes that may be created in the
future have the new default values.
chart_object.set_defaults(axis.X, tic_len=3, tic_interval=50) ax = axis.X() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Lengths are measured by "Postscript points", which coincides with TeX points. One point is equal to 1/72 inch. Several variables and functions are provided to manipulate lengths:
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).
6.2 Plane Orientation
[ < ] | [ > ] | [ << ] | [ 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 samples in a variety of ways.
Samples 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:
l = line_plot.T(data=[(10,20), (11,38), (12,29)], xcol=0, ycol=1) |
In the above example, three sample points will be drawn along with line
segments that connect them: (10, 20) - (11, 38) - (12, 29).
Attribute xcol
tells the locations of X values within data (the
first column of each sample in data
), and ycol
similarly
tell the locations of Y values (the last 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
, except that it reads from
an open file handle FP.
fp = open("foo", "r") data = chart_data.fread_csv(fp, ",") |
read_csv
, but it 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 specify the first and last X values, and
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 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 rows specified in the argument list.
data2 = chart_data.extract_rows([[10,20], [30,40], [50,60]], 1, 2) # data2 will become [[30,40],[50,60]]. |
Extract columns specified in the argument list.
data2 = chart_data.extract_columns([[10,20], [30,40], [50,60]], 0) # data2 will become [[10],[30],[50]]. |
Apply 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 size of a chart. It also
defines the coordinate system (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 18. Fill Styles. |
border_line_style |
line_style.T | None | Line style of the frame that surrounds the chart. See section 15. Line Styles. |
legend |
legend.T | None | The legend of the chart. See section 14. Legends. |
loc |
(x,y) | (0, 0) | Bottom-left corner of the chart. |
size |
(x,y) | (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 9. Axes.. |
x_category_col |
int | 0 | Selects values from 'x_category_data'. |
x_category_data |
list | None | Meaningful only when x_coord_system = '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 |
(x,y) | None | The minimum and maximum X values. |
y_axis |
axis.Y | None | The Y axis. See section 9. 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 |
(x,y) | None | The minimum and maximum Y values. |
y_zap |
(x,y) | None | Compress the section. |
y_zap_entire_area |
int | None | If 1, then the entire area is horizontally `zapped'. |
Objects of area.T
also define several methods:
plot
must be a plot. See section 10. Line and Scatter Plots, 12. Bar Plots,
13. Pie Plots, 11. Range Plots.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Module Axis defines the look of an axis as well as 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 x_range[0] (or y_range[0]) of the area. |
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 19. Texts.. |
grid_interval |
Number or function | None | 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 15. Line Styles. |
label |
string | axis label | The description of the axis. See section 19. Texts.. |
label_offset |
(x,y) or None | (None, None) | The location where the axis label is drawn. Relative to the left-bottom corner of the axis. |
line_style |
line_style.T | default | The style of tick lines. See section 15. Line Styles. |
minor_tic_interval |
Number or 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 |
Number or 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 |
(x,y) | (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 x_range[0] (or y_range[0]) of the area. |
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 19. Texts.. |
grid_interval |
Number or function | None | 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 15. Line Styles. |
label |
string | axis label | The description of the axis. See section 19. Texts.. |
label_offset |
(x,y) or None | (None, None) | The location where the axis label is drawn. Relative to the left-bottom corner of the axis. |
line_style |
line_style.T | default | The style of tick lines. See section 15. Line Styles. |
minor_tic_interval |
Number or 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 |
Number or 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 |
(x,y) | (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 7. 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 19. Texts.. |
data_label_offset |
(x,y) | (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 22. Error Bars. |
label |
string | ??? | The label to be displayed in the legend. See section 14. Legends., See section 19. Texts. |
line_style |
line_style.T | <function <lambda> at 8155280> | The style of the line. See section 15. Line Styles. |
q_max_col |
int | None | See section 22. Error Bars. |
q_min_col |
int | None | See section 22. 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 7. Input Data. |
y_max_col |
int | None | The height of the errorbar is extracted from this column in data. See section 22. Error Bars. |
y_min_col |
int | 2 | The depth of the errorbar is extracted from this column in data. See section 22. Error Bars. |
ycol |
int | 1 | The X values of sample points are extracted from " YCOL'th column of data. See section 7. 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 line_style
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 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', 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 7. Input Data. |
fill_style |
fill_style.T | default | See section 18. Fill Styles. |
label |
string | ??? | The label to be displayed in the legend. See section 14. Legends., See section 19. Texts. |
line_style |
line_style.T | default | The style of the boundary line. See section 15. 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 7. 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()) if theme.use_color: colors = [ fill_style.dark_sea_green, fill_style.white, fill_style.brown] else: colors = [ fill_style.gray9, fill_style.white, fill_style.gray5] ar.add_plot(range_plot.T(label="foo", data=data, fill_style = colors[0])) ar.add_plot(range_plot.T(label="bar", data=data, min_col=2, max_col=3, fill_style = colors[1])) ar.add_plot(range_plot.T(label="baz", data=data, min_col=3, max_col=4, fill_style = colors[2])) 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 7. 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 19. Texts.. |
data_label_offset |
(x,y) | (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 22. Error Bars. |
fill_style |
fill_style.T | <function <lambda> at 8157b60> | Fill style of each box. See section 18. 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 14. Legends., See section 19. Texts. |
line_style |
line_style.T | default | The style of the outer frame of each box. See section 15. 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)) plot1=bar_plot.T(label="foo", cluster=(0, 3)) plot2=bar_plot.T(label="bar", hcol=2, cluster=(1, 3)) plot3=bar_plot.T(label="baz", hcol=3, cluster=(2, 3)) ar.add_plot(plot1, plot2, plot3) 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)) bar_plot.fill_styles.reset(); plot1=bar_plot.T(label="foo") plot2=bar_plot.T(label="bar", hcol=2, stack_on = plot1) plot3=bar_plot.T(label="baz", hcol=3, stack_on = plot2) 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))) ar.add_plot(bar_plot.T(label="baz", hcol=3, cluster=(2,3))) ar.draw() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Attribute | Type | Default | Description |
arc_offsets |
list | None | |
arrow_style |
arrow.T | None | See section 21. Arrows. |
center |
(x,y) | 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_col |
int | 0 | |
label_fill_style |
fill_style.T | default | See section 18. Fill Styles. |
label_line_style |
line_style.T | None | See section 15. Line Styles. |
label_offset |
number | None | |
line_style |
line_style.T | default | See section 15. Line Styles. |
radius |
number | None | |
shadow |
(xoff,yoff,fill) | 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), label_offset = 25, arrow_style = arrow.a3) 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 18. Fill Styles. |
frame_line_style |
line_style.T | default | See section 15. 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 |
(x,y) | None | Bottom-left corner of the legend. |
right_fudge |
number | 5 | Amount of space right of the legend. |
shadow |
(xoff,yoff,fill) | 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 17. 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 10. 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 18. Fill Styles. |
line_style |
line_style.T | default | See section 15. 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 17. Colors. |
line_interval |
number | 3 | The interval between successive stitch lines. |
line_style |
line_style.T | default | The style of the line. See section 15. 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 its appearance.
from pychart import * import re x = 100 y = 500 def escape_text(str): s = re.sub("/", "//", str) s = re.sub("\\{", "/{", s) s = re.sub("\\}", "/}", s) return s def show_text(str): global x, y canvas.show(x, y, str) e = "/16/C" + escape_text(str) canvas.show(x+200, y, e) y = y - 20 show_text("/16/hLLeft align") show_text("/16/hRRight align") show_text("/16/hCCenter align") show_text("/a20/16/hRAngled text") def show_textv(str): global x, y canvas.show(x, y, str) x = x + 150 y = y - 40 x = 100 show_textv("/16/vT//16//vTTop align") show_textv("/16/vM//16//vT/16Middle align") show_textv("/16/vB//16//vT/16Bottom align") y = y - 40 x = 100 show_text("/16/HHelvetica") show_text("/16/CCourier") show_text("/16/NHelvetica-Narrow") show_text("/16/PPalatino-Roman") show_text("/16/BBookman-Demi") show_text("/16/AAvantgarde") show_text("/16/T/iTimes-Italic") show_text("/16/SSymbol") |
Sequence | Meaning | ||
/a dd |
Specifies the angle the text is drawn. Parameter dd is a number
between -360 to 360. Value 0 means left-to-right text, 90 means
bottom-to-up, etc.
If you want to print numbers right after an angle specification, put
{} between the angle and the number. For example, the below
code shows string ""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 font size to dd points. |
"/20{}2001 space odyssey!" |
Restriction: `/h', `/v', and `/a' must appear at the beginning of a string.
The font
module defines several procedures and variables for
manipulating fonts:
[ < ] | [ > ] | [ << ] | [ 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 18. Fill Styles. |
left_fudge |
number | 5 | Space left of the box |
line_style |
line_style.T | default | Frame box style. See section 15. 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 |
shadow |
(xoff,yoff,fill) | None | |
text |
string | ??? | Text body. See section 19. Texts. |
top_fudge |
number | 0 | Space above the first line |
In addition to the above attributes, it provides the following methods.
This method adds a straight arrow 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 vertex of the text box. `arrow' specifies the style of the arrow. (see section 21. 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 17. 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 | default | Line style. See section 15. 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 | default | See section 15. Line Styles. See section 15. Line Styles. |
tic_len |
number | 10 | Length of the horizontal bars |
Attribute | Type | Default | Description |
hline_style |
line_style.T | default | The style of the horizontal bars. See section 15. Line Styles.. See section 15. 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 15. Line Styles. |
Attribute | Type | Default | Description |
line_style |
line_style.T | default | See section 15. Line Styles. |
Attribute | Type | Default | Description |
box_width |
number | 4 | |
fill_style |
fill_style.T | gray7 | See section 18. Fill Styles. |
line_style |
line_style.T | default | See section 15. Line Styles. |
tic_len |
number | 4 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
First, you need to tell PyChart{} that you are using colors by giving option "`color=yes'" in the command line:
% PYCHART_OPTIONS="color=yes" % export PYCHART_OPTIONS % python foo.py |
This option has several effects:
colors.standards
) will include all
the colors listed in X's RGB file. So you can use something like
"`color.slate_blue'"(1)
fill_style.standards
) will be colorized.
Thus, if you create a bar plot without explicitly specifying its fill
style, you will see colored bars on the canvas.
line_style.standards
) will be colorized.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
24.1 Drawing Arbitrary Objects on Canvas 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 segment 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 19. 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 on Canvas) 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 O P Q R S T U V W X Y |
---|
Jump to: | A B C D E F G H I J L M O P Q R S T U V W X Y |
---|
[Top] | [Contents] | [Index] | [ ? ] |
In fact, you can use these color names in greyscale mode as well. They will appear in some gry color though.
[Top] | [Contents] | [Index] | [ ? ] |
[Top] | [Contents] | [Index] | [ ? ] |
1. Introduction
2. Examples
3. Anatomy of a Chart
4. PyChart Options
5. Attributes
6. Coordinate System
7. Input Data
8. Area
9. Axes
10. Line and Scatter Plots
11. Range Plots
12. Bar Plots
13. Pie Plots
14. Legends
15. Line Styles
16. Tick Marks
17. Colors
18. Fill Styles
19. Texts
20. Annotation
21. Arrows
22. Error Bars
23. Generating Color Plots
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 |