Visualize Xml Data With Python And Matplotlib

Python, numpy, matplotlib, and lxml are a powerful toolset for creating plots from XML data.

Matplotlib is a relatively new package in 2d visualization, but it appears to get most things right. It is programmed as a package for python. This means that one can use python libraries to extract and process data before creating the high quality plots. Python comes with quite nice XML tools too so Its easy to get data out of XML files, — If you know how.

This snipped reads in the dos.xml and creates a dos plot.
It works as it is here in ipython which is the preferred shell to do this kind of visualization
For xml processing the prefered library is libxml2, python has a interface for that. On Debian like systems the package is called python-libxml2.

(install pylab matplotlib and ipython)


ipython -pylab

then in the ipython shell:
import libxml2
from libxml2 import xmlAttr
doc = libxml2.parseFile("dos.xml")
ctxt = doc.xpathNewContext()
x= map(float,map(xmlAttr.getContent,ctxt.xpathEval("//totaldos/diagram/point/@distance")))
y= map(float,map(xmlAttr.getContent,ctxt.xpathEval("//totaldos/diagram/point/@totaldensity")))
title('total DOS Aluminum')
xlabel('energy [a.u.]')
fill_between(x, 0, y)
axis([-0.50, 0.5, 0, 30])

recognize this line

x= map(float,map(xmlAttr.getContent,ctxt.xpathEval("//totaldos/diagram/point/@distance")))

the string

is xpath. That means it is possible to use all the functionality of the xpath language to get exactly the wanted data.

xpath links:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License