How to draw different shapes with Python using Graphviz - python

I tried to draw structure graph with Python. Nodes are like this
.
Since I also need to use different shape of arrows. So I wanted to use Graphviz to implement this. Is there any method to draw like this?

Many people seem to use PyPl (https://pypi.org/project/graphviz/) to interface Python to Graphviz. Read all the installation instructions & remember to install the actual Graphviz system.
There are two ways to build node structures, I recommend HTML-like nodes (https://graphviz.org/doc/info/shapes.html#html) (a bit wordy, but fewer layout restrictions)

There are few options to interface Graphviz with python:
graphviz
pygraphviz
graphviz-python
pydot
You can use Records-based Nodes or HTML-based Nodes to get the intended result:
import graphviz
g = graphviz.Digraph('G')
g.node('n1', '{name|{a|b|c}}',
shape='record',
fillcolor='skyblue',
style='filled')
g.view()

Related

Embed LaTeX in Graphviz

I am creating a graph using python and graphviz.
So my script goes like :
from graphviz import Digraph
dot = Digraph()
dot.node('Start')
dot.node('Calculate A')
dot.edge('Start', 'Calculate A')
and so on.
I need to write math text in some nodes, let's say, replace 'A' by an integral. How can that be done?
Thanks in advance,
M
Sorry, Graphviz does not support LaTeX.
In theory, you could use Unicode & Graphviz (pseudo) HTML to create what you want, but it would probably be quite challenging.
A (pretty) easy way to combine LaTex and Graphviz would be to create separate image files (svg, png, jpeg, ...) - one per node using your "usual" LaTeX processes and then "including" these files in your Graphviz program using the image attribute.

Displaying horizontal flowcharts in pydot

I am using python module for pydot to make flowcharts. But the flowcharts are being generated in a vertical fashion. I want them to be generated in a horizontal way. Is there any way that this can be achieved?
You need to give your graph the rankdir=LR attribute, as shown in this Graphviz forum post.
I don't know pydot (but I have written a few programs that create graphs in the DOT language), so I don't know the exact syntax you need to use, and I'm having trouble locating docs for pydot, but it appears that you can just pass the attribute as a keyword in the Dot constructor, eg
dot_object = pydot.Dot(graph_name="main_graph", rankdir="LR", ...)

Fixed position hierarchical output from NetworkX without graphviz?

I'm trying to use Python to plot simple hierarchical tree. I'm using the networkx module. I declared a simple graph
G=networkx.DiGraph()
After adding nodes and edges into G, I tried using
nx.draw(G)
or
nx.draw_networkx(G)
to plot. The output plot hierarchy are all correct, but the position of the nodes appears all random on the graph. Even worse, each time I ran the script, the node position are different.
There is a solution provided in a similar question which requires graphviz package.
pos=nx.graphviz_layout(G,prog='dot')
nx.draw(G,pos,with_labels=False,arrows=False)
Unfortunately I'm not allowed to install graphviz. So I'm seeking alternative solution here.
From graphviz solution, it looks like it's only needed to compute position of the node. Is it correct to say that, as long as I specify a list of coordinates to the draw command, I would be able to plot node at the correct location?
Thanks
UPDATE (15 Apr 2015) Look at my answer here for code that I think will do what you're after.
So networkx doesn't make it particularly easy to use the graphviz layout if you don't have graphviz because it's a better idea to use graphviz's algorithm if you're trying to reproduce graphviz's layout.
But, if you're willing to put in the effort to calculate the position for each node this is straightforward (as you guessed). Create a dict saying where each node's position should be.
pos = {}
for node in G.nodes():
pos[node] = (xcoord,ycoord)
nx.draw(G,pos)
will do it where xcoord and ycoord are the coordinates you want to have.
The various plotting commands are described here

How to draw multiple edges between same set of nodes in PyGraphviz? [duplicate]

I try to draw multigraph in Python using graphviz.
For now I can draw usual graphs in Python somehow like:
import pygraphviz as pgv
G=pgv.AGraph()
G.add_node('a')
G.add_node('b')
G.layout()
G.add_edge('a','b','first')
G.add_edge('a','b','second')
sorted(G.edges(keys=True))
G.draw('file.png')
And I get on the output:
But actually I want get multigraph, i.e.
But documentation stays that it should differentiate :
I have no idea about drawing multigraph but not just graph.
Thanks for any help.
Addition:
it seems that there are no yet such libraries in python that can do it, so I did it using Wolfram Mathematica. But question is still opened.
Addition
Now working code looks so:
import pygraphviz as pgv
G=pgv.AGraph(strict=False)
G.add_node('a')
G.add_node('b')
G.layout()
G.add_edge('a','b','first')
G.add_edge('a','b','second')
sorted(G.edges(keys=True))
G.draw('file.png')
As the documentation you quoted says, you need to specify strict=False when creating a multi-edge graph. Since you didn't do this your graph doesn't support parallel edges.

Tool to create a python GUI for graph construction

I need to create a GUI for graph construction ("graph" as an abstract representation of a set of objects, not a visual representation of data). The interface will provide a choice of ~5 vertex types and of ~5 edge types. Each vertex will have two data fields: a text label and a file name, which need to be easily editable.
I'm familiar with igraph and have a lot of code written in it. I will use igraph to manipulate the graphs created with this GUI.
Since this will be my first GUI, I'm completely ignorant of what tools are available. Can you please suggest a free library, knowing that eventually the program will need to work on Windows?
EDIT
it seems from the answers I get that I wasn't clear enough. I'm not looking for a way to visualize a graph, but rather for a way to visually create one. By visually, I mean not needing to manually create text files or writing code.
Take a look at xdot.py.
From the homepage
xdot.py is an interactive viewer for graphs written in Graphviz's
dot language.
It uses internally the graphviz's xdot output format as an
intermediate format, and PyGTK and Cairo for rendering.
xdot.py can be used either as a standalone application from
command line, or as a library embedded in your python application.
I like networkx,
from networkx import draw, Graph
from pylab import show
g = Graph()
g.add_edges_from([(1,2),(1,3),(2,4),(2,5)])
draw(g)
show()
which gives,
The only quirk is the requirement for matplotlib to get builtin plotting to work.
If you use python, I think PyQt is a good selection.
What you have to install is listed below:
install Python from here
install PyQt4 from here
But it takes many lines to write GUI application,
it is sometimes better to generate an image to display with image viewer.

Categories

Resources