Showing posts with label SVG. Show all posts
Showing posts with label SVG. Show all posts

Wednesday, June 10, 2015

pygal, a Python SVG charting library

By Vasudev Ram




I came across pygal recently. It is a Kozea project. They also have some other interesting projects, some of which are in Python. lxml is the only needed dependency for pygal.

I tried out a simple example from the pygal site, which generates a graph of Fibonacci numbers, and then enhanced it a bit to add a graph of a univariate quadratic function, which represents a parabola, in the same plot. The quadratic function I used was:

3x^2 + 2x + 4 (three x squared plus two x plus four)

[ Also check out:

Quadratic equations

and

the quadratic formula

if not known (or not remembered from high school mathematics :)

And speaking of quadratics (functions, formulae, equations and so on), also check out my earlier post:

Bhaskaracharya and the man who found zero, which briefly mentioned Brahmagupta, an ancient Indian mathematician, who is supposed to be the discoverer of the number zero, without which practically no modern science or technology (including computers and software :) would be possible. He also gave "the first explicit (although still not completely general) solution of the quadratic equation ax^2 + bx = c", according to the Wikipedia article about him, linked to in the previous sentence.

]

Here is the modified pygal example program, in test_pygal.py:
import pygal

bar_chart = pygal.Bar()
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.render_to_file('bar_chart.svg')

def quadratic(a, b, c, x):
    return a * x * x + b * x + c

bar_chart.add('Quadratic', [quadratic(3, 2, 4, x) for x in range(1, 10)])
bar_chart.render_to_file('bar_chart.svg')
I ran it with:
py test_pygal.py
[ Learn more about py here: py, the Python Launcher for Windows ]

And here is a screenshot of the output of running the test pygal program:


You can intuitively see that if you drew a curve joining the tops of the green bars in the screenshot, it would approximate a parabola. Parabolas occur a lot in nature too, as do many other mathematical concepts or principles.

- Enjoy.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Tuesday, March 11, 2014

SVGFig, a pure Python SVG library

By Vasudev Ram


SVGFig is a pure Python library to generate and manipulate SVG images.

I came across it recently and tried it out a little.

Here's an example program I wrote, that uses SVGFig to generate a series of overlapping squares as an SVG image:

# test_svgfig.py
from svgfig import *

svgs = []
colors = [ "red", "green", "blue" ]

for x in range(10, 400, 50):
    y = x
    for j in range(3):
        svg = SVG("rect", x=x + 20 * j, y=y + 20 * j, \
            width=100, height=100, fill=colors[j])
        svgs.append(svg)

g = SVG("g", *svgs, fill_opacity="50%")
print g.xml()

I ran it with the command: python test_svgfig.py

That gave the output shown in the screenshot below (click image to enlarge):



- Vasudev Ram - Dancing Bison Enterprises

Contact Page

Monday, December 31, 2012

Cairo, multi-language, cross-platform graphics library, and PyCairo


http://cairographics.org

The cairo graphics library is an interesting toolkit,  used by many well-known products, including GTK+, Mono, Poppler, Mozilla Firefox, WebKit, R, Gnuplot and others.

It supports graphics output to many targets, including the X Window System (Unix and other platforms), Win32, Mac OS X Quartz, PDF, PostScript, PNG and SVG.

Though written in C, it has bindings for many programming languages, including C++ , Factor, Haskell , Lua , Perl , Python , Ruby, Scheme , Smalltalk and others.

Pycairo is the Python binding to cairographics.

http://en.m.wikipedia.org/wiki/Cairo_(graphics).

According to Wikipedia (see above), one of the founders of the cairo library was Keith Packard, who was also one of the key developers of X-Windows:

http://en.m.wikipedia.org/wiki/Keith_Packard

It was interesting to read that, because I had attended a training program on X, XLib, Xt and Motif, at the company where I worked, years ago; this was before X became widely available on Linux desktop computers; we used HP workstations during the course. I remember having a lot of fun during that course, along with the other participants. We kidded each other and the instructor a lot. Who can forget "DC the DC; Window theWindow", etc. :)
(DC being Device Context). Those were C / XLib declarations, from an X book used for the course, which might have been by Packard (or Gettys, IIRC).

I also remember a colleague of mine, creating a rudimentary version of a graphical paint program, like MS Paint, in Motif,  during the Motif part of the course, in under an hour or so. That was probably my first introduction to the power of frameworks, and to the concept of callbacks (in C). I don't think I really grokked them at the time, though I did, later.

http://cairographics.org/samples/

I had come across cairo some time ago, and made a note of it, because of its support for PDF output, an area I'm interested in, and also because of its  Python support.

I saw it again today via the page  below on preshing.com (which is the same site where I saw the info for my recent post on Mandelbrot graphics and Python):

http://preshing.com/20110920/the-python-with-statement-by-example

The above page gives a nice example of the use of Python's with statement, in the context of using PyCairo to draw some rectangles, each rotated by some angle from the previous one, without the transformations being additive (in a sense - see the post for details).

- Vasudev Ram

Sunday, December 16, 2012

D3.js looks interesting

D3.js - Data-Driven Documents

I have been seeing the D3.js JavaScript library mentioned on tech sites for some days. Took a look at it today. Seems interesting and useful.

It uses a functional style, at least in parts, which can result in shorter and clearer code, something like the difference between SQL and earlier C-ISAM based approaches to data handling.

D3.js is  an abstraction over the DOM but still gives you access to the underlying HTML, CSS and SVG elements.

The D3 Gallery:

https://github.com/mbostock/d3/wiki/Gallery

- Vasudev Ram
www.dancingbison.com