Graphs and django

50 views
Skip to first unread message

Gerry Steele

unread,
Jun 20, 2007, 11:26:24 AM6/20/07
to django...@googlegroups.com
i was wondering if anyone had any experience integrating any python
graphing or anything similar libraries into django and which library
people might recommend.

My needs are to create line, bar and pie charts.


Many Thanks
Gerry
--
Gerry Steele
http://belfast.no-ip.info/

Joseph Heck

unread,
Jun 20, 2007, 11:48:41 AM6/20/07
to django...@googlegroups.com
I've been using FusionGraphs (flash based graphing, generating the
"data" through XML templates) with good success. It's not directly in
python using PIL or anything, but it's been quick and effective.

-joe

Martin Winkler

unread,
Jun 20, 2007, 11:55:36 AM6/20/07
to django...@googlegroups.com
Am Wed, 20 Jun 2007 16:26:24 +0100
schrieb "Gerry Steele" <gerry....@gmail.com>:

> My needs are to create line, bar and pie charts.

One fast way for line graphs would be plain PIL
(http://www.pythonware.com/products/pil/) module. But the results are
usually not really pretty: No antialiasing of lines, except you also use
the aggdraw module (http://effbot.org/zone/aggdraw-index.htm).

If you want do do really nice graphs, you might take a look at some
screenshots of matplotlib:
http://matplotlib.sourceforge.net/screenshots.html

The module is quite large, but has really many functions: from simple
pie charts (3D too) up to finance charts which gets data from
finance.yahoo.com

I took quite some time until I figured how matplotlib/pylab works, but
eventually I succeeded. Hopefully you'll get to some good results
faster than me :)

Martin

Kent Johnson

unread,
Jun 21, 2007, 7:20:10 AM6/21/07
to django...@googlegroups.com
Martin Winkler wrote:
> If you want do do really nice graphs, you might take a look at some
> screenshots of matplotlib:
> http://matplotlib.sourceforge.net/screenshots.html
>
> The module is quite large, but has really many functions: from simple
> pie charts (3D too) up to finance charts which gets data from
> finance.yahoo.com
>
> I took quite some time until I figured how matplotlib/pylab works, but
> eventually I succeeded. Hopefully you'll get to some good results
> faster than me :)

I had a similar experience with matplotlib. The docs are not always too
easy to understand so you have to do some digging and experimenting but
it is very flexible and powerful and the resulting charts are good quality.

A recipe here shows how to return a matplotlib image in a Django response:
http://www.scipy.org/Cookbook/Matplotlib/Django

Kent

Chundermonkey

unread,
Jun 21, 2007, 9:10:39 AM6/21/07
to Django users

i've used chartdirector in the past with django, very easy to install,
although the free version cannot be used for commercial purposes.

Toby Dylan Hocking

unread,
Jul 11, 2007, 3:41:31 AM7/11/07
to Gerry Steele, django...@googlegroups.com
Hi there,

If you still need help with making data-driven plots, why don't you check
out my new Django package --- I'd like some testers and comments.
Basically it is an interface to the R programming language (for statistics
and graphics) through the RPy package. You can download my latest release:

http://www.ocf.berkeley.edu/~tdhock/dataplot-0.2.tgz

I'm a statistician who has been using this system for some time at my
work, but I finally am getting around to generalizing it and packaging it
for general use with the Django framework.

The installation instructions are in INSTALL.txt in the archive.
Documentation is mostly present in docstrings at the moment -- I'm working
on more tutorials, but the .txt files and the example app should be enough
to get you started.

Sincerely,
Toby Dylan Hocking
http://www.ocf.berkeley.edu/~tdhock

Ben Ford

unread,
Jul 11, 2007, 4:36:26 AM7/11/07
to django...@googlegroups.com
Hi Toby
I've grabbed a copy of your code but the RPlot.get_r_fun method isn't working for me... Could you perhaps explain how it does the mapping to the R function? What my testing is saying to me is that r (the one that's imported at the top of __init__.py) has no attribute generic... I've had a very brief look at rpy before, but I'm not exactly familiar with it's inner workings....
Great work, I've been meaning to have a crack at something like this for ages!!
Ben

Toby Dylan Hocking

unread,
Jul 11, 2007, 8:04:29 PM7/11/07
to django...@googlegroups.com
Hi Ben,

I'm excited to hear that you got a copy of django.contrib.dataplot and you
are trying it out. I will try to help you debug the problem, but the
full traceback may be more useful. Can you send it?

The purpose of the get_r_fun method is to look at the current R
environment and check if the desired R function has been source'ed yet.
This is one of the first steps before passing the data before R.

I take the "r has no attribute generic" to refer to one of the generic
plotting functions named in one of the RPlot subclasses -- you must be
dealing with Scatter, TimeSeries, or NormalQQPlot, right? Are you sure it
doesn't say something like "r has no attribute generic.scatter.plot"? You
might try changing the dots . to underscores _ in the
r_fun_name='generic.scatter.plot' line in the definition of the
ScatterPlot subclass.

Maybe your version of RPy isn't translating python names into R names the
same way as mine is? I'm using RPy 0.4.6-3ubuntu2 Python 2.4.2-0ubuntu3
and R 2.2.1-2 on ubuntu dapper. What are your versions?

Sincerely,
Toby Dylan Hocking
http://www.ocf.berkeley.edu/~tdhock

> --
> Regards,
> Ben Ford
> ben.f...@gmail.com
> +628111880346
>
> >
>

Jeremy Dunck

unread,
Jul 11, 2007, 8:29:22 PM7/11/07
to django...@googlegroups.com, Gerry Steele
On 7/11/07, Toby Dylan Hocking <tdh...@ocf.berkeley.edu> wrote:
> If you still need help with making data-driven plots, why don't you check
> out my new Django package --- I'd like some testers and comments.

Initial reaction-- sweet!

After linking dataplot into my contribs dir and adding both dataplot
and dataplot.bike in my INSTALLED_APPS, I get this:

jdunck@jdunckpeg2:~/work/pegasus/b-schools$ ~/django-admin.py syncdb
RHOME= /usr/lib/R
RVERSION= 2.4.1
RVER= 2041
RUSER= /home/jdunck
Loading Rpy version 2041 .. Done.
Creating the R object 'r' .. Done
Error: Couldn't install apps, because there were errors in one or more models:
bike.ride: "distance": FloatFields require a "decimal_places" attribute.
....

A quick check indicates that this is because of a backwards incompat in Django:
http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#RenamedFloatFieldtoDecimalField

I guess you're running on latest trunk?

Ben Ford

unread,
Jul 11, 2007, 11:16:52 PM7/11/07
to django...@googlegroups.com
Hi Toby,
My versions are:
R:            2.4.0
Python:   2.5.1
Rpy:        1.0-RC2

Sorry I wasn't very clear earlier... I imported r into ipython to have a play with it and see what happens. I tried getattr(r,' generic.scatter.plot') which didn't work - I'm under the impression that getattr won't work with dotted notation like that if you're doing normal object lookup, am I right in saying that r does it differently to python? So i tried getattr(r, 'generic') and that didn't work either. The error I get is:
> Error in get(x, envir, mode, inherits) : variable "generic.time.series" was not found
I tried it with underscores too and I got exactly the same message...

In fact just having looked through the source and having a play with rpy it seems that there are bugs all over the place:

>>> from rpy import r
RHOME= /usr/lib/R
RVERSION= 2.4.0
RVER= 2040
RUSER= /home/ben
Loading Rpy version 2040 .. Done.

Creating the R object 'r' ..  Done
>>> r
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/rpy.py", line 307, in __repr__
    Rver = self.__getitem__('R_version_string')
  File "/usr/lib/python2.5/site-packages/rpy.py", line 290, in __getitem__
    obj = self.__dict__[name] = self.__dict__.get(name, self.get(name))
rpy.RException: Error in get(x, envir, mode, inherits) : variable "R_version_string" was not found

It looks like the logic in R.__getattr__ isn't working properly! I'll reinstall it at some point and see how it works :-)
Cheers
Ben

Toby Dylan Hocking

unread,
Jul 12, 2007, 2:24:48 AM7/12/07
to django...@googlegroups.com
Hi Ben,

I see what you're doing now. There are a couple things I should mention.

First of all, you are right: getattr(r,'generic.scatter.plot') is not the
same as typing r.generic.scatter.plot -- instead, it invokes the
__getattr__ method of r, and looks for something by that name in the R
namespace. Having found nothing, it raises an exception. If it did find
something -- you'd have to rplot.source_for_function() or
r.source("/path/to/generic.scatter.plot.R") first -- it would return the
function, and assign it to r.generic_scatter_plot (rpy automatically
translates R dots . to python underscores _ in variable names).

It also looks like our package versions are significantly different. RPy
is notoriously touchy about what particular package versions you are
using, so that may be the cause of django.contrib.dataplot's failure on
your system. But despite the differences in package versions, I bet that
if you source the R code first, you will be able to execute the getattr
properly, i.e.

>>> from rpy import r
>>> r.source("/usr/local/lib/python2.4/site-packages/django/contrib/dataplot/R/generic.scatter.plot.R")
{'visible': False, 'value': <Robj object at 0xb7e11190>}
>>> r.generic_scatter_plot
<Robj object at 0xb7e111a0>

However, the whole point of django.contrib.dataplot is that it takes care
of these details for you ---

>>> from django.contrib import dataplot
>>> plot=dataplot.Scatter('myscatterplot',fun_to_get_scatter_data)
>>> plot.to_html()

Have you tried to get the demo bike app (django.contrib.dataplot.bike) to
work? If you are able to get those simple examples working, it should be
straightforward to translate the design paradigm to your problems.

Sincerely,
Toby Dylan Hocking
http://www.ocf.berkeley.edu/~tdhock

>> http://www.ocf.berkeley.edu/~tdhock<http://www.ocf.berkeley.edu/%7Etdhock>


>>
>> On Wed, 11 Jul 2007, Ben Ford wrote:
>>
>>> Hi Toby
>>> I've grabbed a copy of your code but the RPlot.get_r_fun method isn't
>>> working for me... Could you perhaps explain how it does the mapping to
>> the R
>>> function? What my testing is saying to me is that r (the one that's
>> imported
>>> at the top of __init__.py) has no attribute generic... I've had a very
>> brief
>>> look at rpy before, but I'm not exactly familiar with it's inner
>>> workings....
>>> Great work, I've been meaning to have a crack at something like this for
>>> ages!!
>>> Ben
>>>
>>> On 11/07/07, Toby Dylan Hocking <tdh...@ocf.berkeley.edu> wrote:
>>>>
>>>>
>>>> Hi there,
>>>>
>>>> If you still need help with making data-driven plots, why don't you
>> check
>>>> out my new Django package --- I'd like some testers and comments.
>>>> Basically it is an interface to the R programming language (for
>> statistics
>>>> and graphics) through the RPy package. You can download my latest
>> release:
>>>>

>>>> http://www.ocf.berkeley.edu/~tdhock/dataplot-0.2.tgz<http://www.ocf.berkeley.edu/%7Etdhock/dataplot-0.2.tgz>


>>>>
>>>> I'm a statistician who has been using this system for some time at my
>>>> work, but I finally am getting around to generalizing it and packaging
>> it
>>>> for general use with the Django framework.
>>>>
>>>> The installation instructions are in INSTALL.txt in the archive.
>>>> Documentation is mostly present in docstrings at the moment -- I'm
>> working
>>>> on more tutorials, but the .txt files and the example app should be
>> enough
>>>> to get you started.
>>>>
>>>> Sincerely,
>>>> Toby Dylan Hocking

>>>> http://www.ocf.berkeley.edu/~tdhock<http://www.ocf.berkeley.edu/%7Etdhock>

Toby Dylan Hocking

unread,
Jul 12, 2007, 2:33:12 AM7/12/07
to django...@googlegroups.com, Gerry Steele
Hey Jeremy,

Thanks for the input. You are correct: I am running a recent copy of
django from SVN on the primary machine I am using to develop
django.contrib.dataplot.

In fact, I ran into the same problem that you did when I tried to port
django.contrib.dataplot to a different machine -- a machine running Django
0.95. After realizing the existence of the new backwards-incompatible
changes, I was able to get the django.contrib.dataplot.bike demo app
working on my old system by simply adding the max_digits and
decimal_places arguments to the bike/models.py FloatFields. Another
solution would have been to upgrade the django libraries to the new trunk
versions.

Again, thanks for trying out my library. I'm anxious to hear if you're
able to get it working. Once you are able to get the bike app working on
your system, it should be straightforward to translate the design
paradigms to your plotting application.

Sincerely,
Toby Dylan Hocking
http://www.ocf.berkeley.edu/~tdhock

Ben Ford

unread,
Jul 12, 2007, 3:50:59 AM7/12/07
to django...@googlegroups.com
Hey Toby,
Thanks for the input, I'll try these things on tuesday (not to make anyone jealous but I'm going to Bali for a long weekend in about 10 minutes :-D ). The problems came up when I followed the steps in one of your .txt files... I haven't had a chance to rigorously explore whats going on yet, and I'll get right on to that when I get back!
As (another) aside I'm developing a reporting framework for django at the moment, which I'd be keen to release to the community, and I think something like dataplot would be a really good partner to that!! I'll be in touch - sunburn not withstanding - when I get back! :-)
Ben

On 12/07/07, Toby Dylan Hocking <tdh...@ocf.berkeley.edu> wrote:

Hi Ben,

I see what you're doing now. There are a couple things I should mention.

First of all, you are right: getattr(r,'generic.scatter.plot') is not the
same as typing r.generic.scatter.plot -- instead, it invokes the
__getattr__ method of r, and looks for something by that name in the R
namespace. Having found nothing, it raises an exception. If it did find
something -- you'd have to rplot.source_for_function () or

r.source("/path/to/generic.scatter.plot.R") first -- it would return the
function, and assign it to r.generic_scatter_plot (rpy automatically
translates R dots . to python underscores _ in variable names).

It also looks like our package versions are significantly different. RPy
is notoriously touchy about what particular package versions you are
using, so that may be the cause of django.contrib.dataplot's failure on
your system. But despite the differences in package versions, I bet that
if you source the R code first, you will be able to execute the getattr
properly, i.e.

>>> from rpy import r
>>> r.source("/usr/local/lib/python2.4/site-packages/django/contrib/dataplot/R/generic.scatter.plot.R")
{'visible': False, 'value': <Robj object at 0xb7e11190>}
>>> r.generic_scatter_plot
<Robj object at 0xb7e111a0>

However, the whole point of django.contrib.dataplot is that it takes care
of these details for you ---

>>> from django.contrib import dataplot
>>> plot= dataplot.Scatter('myscatterplot',fun_to_get_scatter_data)
Reply all
Reply to author
Forward
0 new messages