A library for making charts with Python
Project description
PyCha
Pycha is a very simple Python package for drawing charts using the great Cairo library. Its goals are:
Lightweight
Simple to use
Nice looking with default values
Customization
It won’t try to draw any possible chart on earth but draw the most common ones nicely. There are some other options you may want to look at like pyCairoChart.
Pycha is based on Plotr which is based on PlotKit. Both libraries are written in JavaScript and are great for client web programming. I needed the same for the server side so that’s the reason I ported Plotr to Python. Now we can deliver charts to people with JavaScript disabled or embed them in PDF reports.
Pycha is distributed under the terms of the GNU Lesser General Public License.
Documentation
Installation
Pycha needs PyCairo to works since it uses the Cairo graphics library. If you use Linux you will probably already have it installed so you don’t have to do anything. If you use Windows these are the recommended steps for installing PyCairo:
Grab the latest PyCairo Windows installer from http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/ You need to use the one that matches your Python version so take the one ending in -py2.4.exe for Python 2.4 or the one ending in -py2.5.exe for Python 2.5
Install it in your Python environment (just follow the installation program instructions)
Put the Cairo dlls inside the pycairo directory inside your site-packages directory or anywhere in your path. You can find the dlls at http://www.gimp.org/%7Etml/gimp/win32/downloads.html Go there and download the following packages:
cairo.zip. You just need the libcairo-2.dll file inside that zip
libpng.zip. You just need the libpng13.dll file inside that zip
zlib.zip. You just need the zlib1.dll file inside that zip
Pycha is distributed as a Python Egg so is quite easy to install. You just need to type the following command:
easy_install pycha
And Easy Install will go to the Cheeseshop and grab the last pycha for you. If will also install it for you at no extra cost :-)
Tutorial
Using pycha is quite simple. You always follow the same 5 simple steps:
Create a Cairo surface to draw the chart on
Build a list of data sets from which your chart will be created
Customize the chart options.
Create the chart, add the datasets and render it
Save the results into a file or do whatever you want with the Cairo surface
To create the Cairo surface you just need to say the type of surface and its dimensions:
import cairo width, height = (500, 400) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
Then you should create your data set querying a database or any other data source:
dataSet = ( ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))), ('dataSet 2', ((0, 2), (1, 4), (2, 3))), ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))), )
As you can see, each data set is a tuple where the first element is the name of the data set and the second is another tuple composed by points. Each point is a two-elements tuple, the first one is the x value and the second the y value.
Not every chart uses all the information of a data set. For example, the Pie chart only uses the first point of each dataset and it only uses the y value of the point.
Now you may want to specify some options so the chart can be customize changing its defaults values. To see the defaults you can check the pycha.chart.Chart.__init__ method in the source code. You can use regular dictionaries to define your options. For example, imagine you want to hide the legend and use a different color for the background:
options = { 'legend': {'hide': True}, 'background': {'color': '#f0f0f0'}, }
Now we are ready to instantiate the chart, add the data set and render it:
import pycha.bar chart = pycha.bar.VerticalBarChart(surface, options) chart.addDataset(dataSet) chart.render()
Right now you can choose among 4 different kind of charts:
Pie Charts (pycha.pie.PieChart)
Vertical Bar Charts (pycha.bar.VerticalBarChart)
Horizontal Bar Charts (pycha.bar.HorizontalBarChart)
Line Charts (pycha.bar.LineChart)
Scatterplot Charts (pycha.scatter.ScatterplotChart)
Finally you can write the surface to a graphic file or anything you want using the cairo library:
surface.write_to_png('output.png')
That’s it! You can see more examples in the examples directory of the source code.
Documentation
Adam Przywecki has done a fantastic work writing documentation for Pycha. Check it out at http://pycha.yourwei.com/
Development
You can get the last bleeding edge version of pycha by getting a checkout of the subversion repository:
svn co http://www.lorenzogil.com/svn/pycha/trunk pycha
Don’t forget to check the Release Notes for each version to learn the new features and incompatible changes.
Contact
There is a mailing list about PyCha at http://groups.google.com/group/pycha You can join it to ask questions about its use or simply to talk about its development. Your ideas and feedback are greatly appreciated!
Changes
0.5.3 (2010-03-29)
New title color option
Fix crash in chavier application
New horizontal axis lines. Options to turn it (and vertical ones) on and off
Improve precision in axis ticks
Add some examples and update old ones
0.5.2 (2009-09-26)
Add a MANIFEST.in to explictly include all files in the source distribution
0.5.1 (2009-09-19)
Several bug fixes (Lorenzo)
Draw circles instead of lines for scatter chart symbols (Lorenzo)
Error bars (Yang Zhang)
Improve tick labels (Simon)
Add labels with yvals next to the bars (Simon (Vsevolod) Ilyushchenko)
Change the project website (Lorenzo)
0.5.0 (2009-03-22)
Bar chart fixes (Adam)
Support for custon fonts in the ticks (Ged)
Support for an ‘interval’ option (Nicolas)
New color scheme system (Lorenzo)
Stacked bar charts support (Lorenzo)
0.4.2 (2009-02-15)
Much better documentation (Adam)
Fixes integer division when computing xscale (Laurent)
Fix for a broken example (Lorenzo)
Use labelFontSize when rendering the axis (Adam Przywecki)
Code cleanups. Now it should pass pyflakes and pep8 in most files (Lorenzo)
Support for running the test suite with python setup.py test (Lorenzo)
Support for SVG (and PDF, Postscript, Win32, Quartz) by changing the way we compute the surface dimensions (Lorenzo)
0.4.1 (2008-10-29)
Fix a colon in the README.txt file (Lorenzo)
Add a test_suite option to setup.py so we can run the tests before deployment (Lorenzo)
0.4.0 (2008-10-28)
Improved test suite (Lorenzo, Nicolas)
Many bugs fixed (Lorenzo, Stephane Wirtel)
Support for negative values in the datasets (Nicolas, Lorenzo)
Chavier, a simple pygtk application for playing with Pycha charts (Lorenzo)
Allow the legend to be placed relative to the right and bottom of the canvas (Nicolas Evrard)
Easier debugging by adding __str__ methods to aux classes (rectangle, point, area, …) (Lorenzo)
Do not overlap Y axis label when ticks label are not rotated (John Eikenberry)
0.3.0 (2008-03-22)
Scattered charts (Tamas Nepusz <ntamas_AT_rmki.kfki.hu>)
Chart titles (John Eikenberry <jae_AT_zhar.net>)
Axis labels and rotated ticks (John)
Chart background and surface background (John)
Automatically augment the light in large color schemes (John)
Lots of bug fixes (John and Lorenzo)
0.2.0 (2007-10-25)
Test suite
Python 2.4 compatibility (patch by Miguel Hernandez)
API docs
Small fixes
0.1.0 (2007-10-17)
Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pycha-0.5.3.tar.gz
.
File metadata
- Download URL: pycha-0.5.3.tar.gz
- Upload date:
- Size: 39.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b924c2f000a0b36ec4ae72942f70ea17a751db3ad45125800658d4bbc048036 |
|
MD5 | aea1aab42c9d755c74f6d10f3d3c99cb |
|
BLAKE2b-256 | 5342f579803e3d9f031cc449ae5b476a81ba72268b735ba53607b00e73fc373f |