Graphing in Python 3.x - python

In Python 2.6, I used matplotlib to make some simple graphs. However, it is incompatible with Python 3.1.
What are some alternative modules that can accomplish the same thing without being very complex?

You say you want to create some simple graphs but haven't really said how simple or what sort of graphs you want. So long as they aren't too complex you might want to consider using the Google Chart API.
e.g.
That has some advantages: you just have to construct a URL that describes the desired chart so there shouldn't be any issues using it from Python 3.x. Of course there are disadvantages also: you need to have an internet connection when generating the chart, and you might not have the chart styles you have been using with matplotlib.
If you don't want to construct the URLs directly there is at least one Python wrapper for the charts API. It doesn't work directly on Python 3.x, but running it through 2to3 seems to convert it successfully.

A stable version supporting Python 3 has since been released: official announcement.

I wrote a small example that runs in python 3 and uses the google chart api (as suggested by Duncan, I wrote the solution after seeing this post).
It is not ideal since it adds a dependency one a 3rd party that might break backward compatibility at any time, but the graphs are nice and there is absolutely no added dependency on the python code. Worth considering for not 'mission critical code'.
You can find/download the example here. Here is the graph that it generates from data in a .xml file:
# build the query with template parameters
query ="http://chart.apis.google.com/chart?chxl=0:__X_LABELS__&chxp=__X_LABELS_POS__&chxr=0,__MIN_TIME__,__MAX_TIME__|1,__MIN_WEIGHT__,__MAX_WEIGHT__&chxs=0,676767,11.5,0,lt,676767|1,676767,11.5,0,lt,676767&chxt=x,y&chs=800x300&cht=lc&chco=3072F3&chds=__MIN_WEIGHT__,__MAX_WEIGHT__&chd=t:__COMMASEP_WEIGHT__&chdl=Weight&chdlp=b&chls=2,4,1&chma=5,5,5,25&chtt=Your+Weight+Timeline"
[...]
# relace template with data
query = query.replace('__X_LABELS__', strXLabels)
query = query.replace('__X_LABELS_POS__', strXLabelsPos)
query = query.replace('__MIN_TIME__', str(min(lst_dateEpoch)))
query = query.replace('__MAX_TIME__', str(max(lst_dateEpoch)))
[...]
# use 'urllib.request' to download the data & write to file
sock = urllib.request.urlopen(query)
image_bytes = sock.read()
sock.close()
fh = open('Weight_GoogleGraphApi.png', 'wb')
fh.write(image_bytes)
fh.close()

Maybe PyQwt? They claim 3.x compatibility. I've only used Qwt (the C++ lib PyQwt is based on) but I found it fairly useful.

rpy2 is providing access to the graphics capabilities of R, and rpy2 is becoming compatible with Python 3 (thanks to the help of Google for funding Greg over the summer).
Code against the current dev branch is in a patch queue.
edit: rpy2 2.2.0 is working with Python 3.2

MathGL (GPL plotting library) have Python interface which work with Python 3 too.

Matplotlib binaries for python 3.x (windows) are avaliable. http://www.lfd.uci.edu/~gohlke/pythonlibs/

As an alternative to installing subversion to grab the source, Numpy's SF files page has the latest copy of 1.5 in a few different (Windows-friendly) formats:
http://sourceforge.net/projects/numpy/files/NumPy/1.5.0b1/

There are at least two graphing libraries using PyQt, namely PyQwt and PyQtGraph. I've been using PyQwt with Python 2.6 for a few weeks now and it is quite handy. The documentation isn't great, and most of the time I need to refer to either the Qwt docs or the examples - although the times I've had to look at the docs have been few and far between, it is VERY easy to use. I tried building it against python 3.1 just now though without success. I coulnd't find the tar package for 5.2.1 which is the only version compatible with python 3.0 and there isn't anything on MacPorts for that either.
There is also a fairly complete looking list of plotting libraries on at python.org http://wiki.python.org/moin/NumericAndScientific/Plotting

Related

Stitching non-R scripts with knitr, say .py

is it possible to stitch a python script using stitch() with knitr?
I want to produce a simple document by typing:
stitch('someScript.py')
analogous to:
stitch('someScript.R')
As I probably have answered you on Twitter, you can try
library(knitr)
opts_chunk$set(engine = 'python')
stitch('script.py')
But note the Python support in knitr is very limited at the moment. You may or may not be satisfied by it.

Creating excel chart from text file

I am running a simulation that saves some result in a file from which I take the data I need and save it in
result.dat
like so:
SAN.statisticsCollector.Network Response Time
Min: 0.210169
Max: 8781.55
average: 346.966666667
I do all this using python, and it was easy to convert result.dat into an excel file using xlwt. The problem is that creating charts using xlwt is not possible. I than came across Jpype, but installation on my ubuntu 12.04 machine was a headache. I'm probably just being lazy but still - is there any other way, not necessarily python-related, to convert result.dat into an excel file with charts?
Thanks
P.s the file I want to create is a spreadsheet, not Microsoft's Excel!
there is a now a new possibility: http://pythonhosted.org/openpyxl/charts.html
and http://xlsxwriter.readthedocs.org/chart.html
The main problem is that currently there's no Python library that implements MS Excel chart creation, and, obviously, they will not appear due to lack of good chart format documentation (as python-excel.org guys told) and its huge complecity.
There are two other options though:
Another option is to use 3-rd party tools (like JPype that you've mentioned) combining them with Python scripts. As far as I know, except Java smartXML there's no libraries that are capable of creating excel charts (or course, there are ones for .NET, e.g. expertXLS) and I'm not sure it will run on Mono + IronPython, though you can try.
The third option is Win32 COM API, e.g. like described in this SO post, which is not quite an option for you due to your working operating system.

TA-Lib in Ctypes, help calling functions

So for the past three days I've been trying to figure out getting TA-Lib to work with python. This is the source I compiled into a dylib (mac version of a .so) and have been calling it from a python script coded as follows:
from ctypes import *
import numpy
c_float_p = POINTER(c_float)
data = numpy.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
data = data.astype(numpy.float32)
data_p = data.ctypes.data_as(c_float_p)
dylib = CDLL('libta_lib.dylib')
value = dylib.TA_S_SMA(c_int(0), c_int(data.size - 1), data_p, 0, 19, data_p)
Printing value returns 2, no matter what the array values are. I cannot change the fourth argument of TA_S_SMA from 0 or 1, or else I get a python 138 error followed by a python crash. Can anyone explain to me the proper way to call this function? My C skills are limited (read 0).
Useful links:
SMA function source code
Tut I used as a guide
TA-Lib source download link
Thanks!
I had the same problem a couple of weeks ago and I found these instructions. Now they're not using ctypes but it works better in my opinion. SWIG will do all the wrapping for you. A Couple of things to watch out for. When you get to the Single: Multi: sections, if you don't know which on, start with the Multi and if that doesn't work go to the single. A little further down you'll see he's replacing Python 2.3 to Python 2.6. I was using python 2.7 and just replaced the 2.6 with 2.7 and it worked. I'm not sure if this will work for higher versions of python but worth a shot if that's what you're using. Hope it helps.
You should take a look at this TA-Lib Python project.
It uses Cython to wrap TA-Lib and is cross-platform, easy to install, and faster than the SWIG bindings.

Extract Assembly Version from DLL using Python

I'm trying to extract some version information from a DLL using python. I read this question:
Python windows File Version attribute
It was helpful, but I also need to get the 'Assembly version' from the DLL. It's there when I right click and look on the versions tab, but not sure how I extract this with python.
On this page:
http://timgolden.me.uk/python/win32_how_do_i/get_dll_version.html
Tim Golden says:
You can use the slightly more messy
language-dependent code in the demos
which come with pywin32 to find the
strings in the box beneath it.
Can someone point me to the example that might be useful? I looked in the win32api directories but there's nothing obvious. Would I find a solution there?
If you would rather not introduce a dependency on Python.Net, you can also use the win32 api directly:
from win32api import GetFileVersionInfo, LOWORD, HIWORD
def get_version_number (filename):
info = GetFileVersionInfo (filename, "\\")
ms = info['FileVersionMS']
ls = info['FileVersionLS']
return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)
Source: http://timgolden.me.uk/python/win32_how_do_i/get_dll_version.html
I'm not sure you can get at this information by using native code. The usual way of obtaining the assembly info is by running .Net code (e.g. C#). So I'm guessing in order to be able to do the same from python you'll need to run some .Net python interpreter. See for example http://pythonnet.github.io/

Is there a python equivalent of the prefuse visualization toolkit?

The prefuse visualization toolkit is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.
I know this is not exactly python, but you could use prefuse in python through jython
Something along the lines of:
Add prefuse to your path:
export JYTHONPATH=$JYTHONPATH:prefuse.jar
and
>>> import prefuse
from your jython machinery
this guy has an example of using prefuse from jython here
You might want to check out SUMMON, a visualization system that uses python but handles fairly large data sets. There's an impressive video of visualizing and navigating a massive tree. (Can't post the link because I'm a first time poster. It's on the SUMMON front page.)
If you're using a Mac, check out NodeBox. One extension it offers is a graph library that looks pretty good. Poke around in the NodeBox gallery some to find something similar to your problem and it should have some helpful links.
This is well after OP, but just in case:
pydot. Allows generation & rendering of graphs. If you need graph algorithms (transitive closure etc.) also look at pygraphlib which extends and integrates pydot.
Note that neither allows interactive editing of the rendered diagram. They both use graphviz to generate output.
You could try using prefuse with JPype, if you can't find a suitable replacement.
Note that prefuse now has the flare package which uses flash.
Connect that to a Python backend via web2py and you've got a great web app (just an idea).
MayaVi

Categories

Resources