|
|
simpleSVG
|
|
A Python program to produce SVG graphics.
Version 0.1 is new on December 25, 2007.
|
Last modified Tuesday, 08-Jan-2008 17:24:09 MST
return to mensch.org
Why another plotting program?
simpleSVG translates user-friendly
Python commands into an SVG graphics file.
It produces only SVG graphics, and the Python
is adapted to SVG. If you know a bit about SVG,
such as from
svgbasics.com,
the Python commands will be more easily learned and effectively applied.
SVG is a vector graphics format.
I confess here that when I first heard of the term "vector
graphics", I thought it was referring to plots with lots of little
arrows. Nope, that is not what it means! Here are some
definitions of "vector graphics".
simpleSVG is intended to replace vplot,
which produced postscript graphics. Like vplot, simpleSVG
is intended to simplify the use of graphics primitives, to make nonstandard
plots that are not available in traditional plotting programs.
Some links about SVG
Key features of simpleSVG
- SVG attributes such as stroke-width are written as
stroke_width when used as a keyword in a argument list.
simpleSVG will replace the _ with a - when writing the file.
- The SVG editor/viewer inkscape
(excellent cross-platform free software, highly recommended as a companion to
simpleSVG), seems to concatenate style attributes.
Instead of stroke="blue", stroke-width="3" we see
style="stroke:blue;stroke-width=3".
v0.1 of simpleSVG uses the latter form,
concatenated style strings. That may not have been a good idea...
- simpleSVG ultimately writes the files in standard SVG coordinates in "pts",
with the origin in the top left, the first coordinate increasing to the right
and the second coordinate increasing down. Coordinates passed as integers
are assumed to be in units of "pts", long integers in units of 1/100 pts.
(256 and 25600L) are the same coordinate.
- Coordinates passed as floats are assumed to be in "user coordinates":
the conventional x and y with an origin adjacent to the lower left and
bottom margins. (You may see SVG "user coordinates" defined differently in
SVG documentation, here the term refers to the simpleSVG implementation.)
When simpleSVG writes to a file, user coordinates are converted to
SVG coordinates, according to the linear transformation defined by the user.
Installing simpleSVG
Here it is:
Installing simpleSVG.py can be as simple as keeping a copy in the
same directory as the Python programs that will import it. Or keep
it in a directory accesible by your PYTHONPATH.
Using simpleSVG
-
Invoke ipython (or idle). The >>> of
the Python interpreter will appear. At those prompts, enter:
import simpleSVG
a=simpleSVG.svg_class()
a.circle(.5,.5,100,fill='red',stroke='none')
a.rect2(0., .15, 1., .85)
a.close()
^D
You will produce a file temp.svg, which can be viewed with inkscape.
Another test is to simply execute the module simpleSVG.py, which
by default runs an enclosed test program. The
following SVG graphics, testSVG.svg, is produced:
If you do not see an image above, your browser may not be able to display SVG.
Here is the SVG converted to a PNG:
You may wonder what is the benefit of displaying SVG directly to a browser.
Maybe not much. Currently, the rare use of SVG in web pages is to
demonstrate the use of SVG in web pages, which doesn't seem to be
very useful! This page emphasizes using SVG as a vehicle to make
scientific publication-qualility graphics, ultimately in PNG or PDF format.
Except for the one example above, the graphics will be served as PNG.
simpleSVG examples
|