arminstraub.com

Simply Draw

Note: This page is hopelessly outdated. As a result, the code may no longer work as described. Proceed with caution.

What is SimplyDraw?

In short: Simply Draw let's you write easy (or complex) images via very little lines of Python code. It is a layer to the great Python module PyX. Yet it is a bit incomplete and includes merely code that I needed myself.

Suppose you just want to draw a circle with a radius of 1cm. Then all you have to do is: create a file e.g. named circle.py containing the single line stroke(circle(0, 0, 1)) and create the image file circle.eps via:

$ simplydraw circle.py

For further demonstrations you should take a look at the samples.

Changelog

 Simply Draw 0.2.1 2005/09/12Armin Straub
*Rearranged the files a bit.
*Use code from pyx.box in label() function (thanks to Andre Wobst).
*Made point() configurable.
*Tried to use lengths with appropriate length types in point(), stack(), ...
 
 Simply Draw 0.2.0 2005/09/11Armin Straub
*Complete restructuring. Should be cleaner and simpler to maintain.
+Implemented angle and labelsep parameter of label().
*Allow functions like label(), point() or stack() to be called as object methods of a canvas.
*simplydraw script can now handle multiple files.
 
More ...

Download

Simply Draw currently consists of the directory simplydraw which in turn contains

  • the directory simplydraw, which should be placed so that Python can import from it,
  • the script simplydraw.py, which should be linked to (e.g. ln -s /path/to/simplydraw.py simplydraw) from a place where executables are found (if necessary you have to set the execution bits)
  • and a changelog file.

Note that Simply Draw depends on a recent installation of PyX.

Comments and suggestions are as always very welcome.

LinkSizeDescriptionHits
5.98 KB Simply Draw 792

Motivation

When I was writing an article about Gaussian Random Functions, I also wanted to include some small graphics that were drawn on the blackboard by Prof. Dr. M. Lifshits. So what tool to use?

It should fulfill the following objectives (where order changes from application to application...):

  • Drawing easy graphics (such as they are drawn on a blackboard with a few strokes) should be easy with this tool, too.
  • It should be possible to create fancy graphs and able to plot functions with very little code or interaction.
  • The tool should not be static (in the sense that one creates completely fixed pieces of pixels) but make it easy to modify created graphics (e.g. by adjusting some parameters in the code). Imagine that you want to depict the concept of an incircle of a triangle. You draw first the triangle and then the incircle, but then you are inconfident with the used triangle and you want to change it. Then you should be able to do this without redrawing the incircle as well.
  • The tool should be able to handle repetitive tasks. You see, the tool just has to be some kind of a scripting language, where this could be easily handled using loops.

Alternatives

I had already tried (and used to some extend) the following alternatives:

  • When working with TeX it is only a matter of time to give pstricks a try. This is an additional, quite well documented package that does a good job right inside your TeX file. Unfortunately it therefore shares TeX's weaknesses as well. It is hard and not obvious (from my point of view) to include looping techniques or something as simple as just to define a variable (I know how to do it, that's not the point, but I don't like the way and I always have to look at some sample code to remember).
  • I have also used eukleides and I'm quite impressed of the simplicity to write nice graphics with just a few lines if code. But eukleides has not been updated for some time and it is also missing features as control structures or nice loops.

Looking at PyX

Currently my favourite scripting language is Python. I therefore looked out for nice graphics modules that can be used from Python. There are quite a lot, but most of them lacked some serious feature (or I supposed them as nasty).

But then I got stuck looking at PyX: it implemented almost all of my objectives. What it still lacked was:

  • It is not really a matter of a few intuitive statements to draw some simple graphic. First you have to import PyX as usual. Then you have to create a canvas on which will be draw (that also means you have to remember that you need a canvas for drawing). And finally you have to write the canvas explicitly to some files.
  • PyX is well divided into lots of modules and submodules. This is nice, but requires you to write e.g. pyx.graph.axis.painter.ticklength if you want to access a ticklength.
  • It's very hard (supposed you have to do it for the first time) to draw a graph in the typical mathematical way (two axes meeting in zero and having arrows at one side). In fact it was not even possible to do this easily in PyX prior to version 0.8.

Simply Draw

My attempt to solve the issues with PyX lead to Simply Draw.

  • It imports all the interesting stuff from PyX (there might still be a lot missing) in a way that you can use it more easily and don't have to remember from which module it came from. You can e.g. use line instead of pyx.path.line.
  • You don't have to import anything. You just write the code for your graphic.
  • There is no need to create a canvas before drawing. There exists a global one (more or less hidden) and you can just call e.g. stroke(circle(0, 0, 1)).
  • You don't have to write the graphic yourself via a call to writeEPSfile. This is done by eventually calling
    $ simplydraw yourcode.py
    which by default would produce yourcode.eps in the same directory.
  • Simply Draw introduces some additional helper functions such as intersections or stack that ease common procedures. Also included is mathxy, an alternative to graphxy, that provides a mathematical graph as mentioned above by default. Note that there is still a lot to do in this respect e.g. enhancing the function point.
  • Simply Draw allows data.function() and data.paramfunction() to take real Python expressions as a parameter. It also provides the graph style style.steps.