user506710
user506710

Reputation:

Drawing mathematical stuff with Python

I had asked in my previous question for a package that lets me draw circles and arrows with python and it was suggested that I should try out PyQt4.

My problem is that I have tried to find functions that would let me draw such widgets but have been unable to do so.

So my question is that if anybody knows how to draw circles,arrows and other mathematical symbols in PyQt4 please guide me in the right direction.

btw if anyone of you knows of any package in Python that would let me do so please help.

I need to draw NFA's in my program and thus would need this.

http://en.wikipedia.org/wiki/File:NFAexample.svg

Thanks a lot


As an update I would like to mention that I am not only interested in PyQt4 . If you are aware of any such package that can let me draw mathematical GUI with python please help :)

Upvotes: 0

Views: 2329

Answers (3)

unutbu
unutbu

Reputation: 879291

Using graphviz you can make diagrams like this

alt text

using code like this:

#!/usr/bin/env python
# coding: utf-8

import os
import subprocess
import shlex

# dot code based on http://www.graphviz.org/Gallery/directed/fsm.html
dot='''    
digraph finite_state_machine {
        bgcolor="#ffffff"
        rankdir=LR;
        size="8,5"
        node [shape = doublecircle]; S₁ S₃;
        node [shape = circle]; S₀ S₂ S₄;
        node [shape = circle]; S₄; 
        S₀ -> S₁ [ label = ε, weight=-1 ];
        S₀ -> S₃ [ label = ε, weight=-1 ];
        S₁ -> S₂:nw [ label = 0 ];
        S₂ -> S₁:se [ label = 0 ];
        S₁ -> S₁ [ label = 1 ];
        S₂ -> S₂ [ label = 1 ];
        S₃ -> S₄:nw [ label = 1 ];
        S₄ -> S₃:se [ label = 1 ];
        S₃ -> S₃ [ label = 0 ];
        S₄ -> S₄ [ label = 0 ]; 
}
'''
os.chdir('/tmp')
with open('output_dot.svg','w') as outfile:
    subprocess.Popen(shlex.split('dot -Tsvg'),
                     stdin=subprocess.PIPE,stdout=outfile).communicate(dot)

Usually, one calls the graphviz dot program from the command-line. It has nothing to do with Python, per se.

To make diagrams programmatically, however, you could use Python to construct the dot code as a giant string, and use subprocess.Popen to process the string through dot. dot can output in many formats, including SVG or PNG. Or, you may want to research pygraphviz for a more "Pythonic" interface to the graphviz library.

Also note that graphviz supports UTF-8 encoded characters, so you can include subscripts and other math symbols in your diagram by finding the appropriate symbols in the UTF-8 character set.

One of graphviz's strengths is that you don't have to tediously specify the layout of the diagram -- you just say what nodes are connected to what other nodes. The location of the nodes is determined by graphviz (mostly) automatically.

This strength is also somewhat debilitating -- it's hard to place elements in the diagram exactly as you might wish. Notice, for example, that S1 and S2 are not horizontally aligned. I also don't know how to make the arrow from S0 --> S1 more prettily curved, arrows between S1 and S2 perfectly symmetric, or how to make the edge label from S2 --> S1 appear below the arrow, etc... There might be a way to do all these things, but I don't know how.

For more examples of what graphviz can do, see the graphviz gallery.

Upvotes: 4

user59634
user59634

Reputation:

You might want to take a look at NetworkX. Might just suit your needs! Since you are looking to draw NFAs.The gallery shows some of the things that can be done.

Upvotes: 1

DGH
DGH

Reputation: 11539

Take a look at this part of the API: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qgraphicsitem.html

I believe that class and its subclasses will do what you want. For some things, like arrows, you may have to construct them out of existing elements such as lines or define your own subclass of qgraphicsitem that does what you want.

Upvotes: 0

Related Questions