3

Very simple, I am trying to plot a simple state-machine like this:

enter image description here

I am open to any form of plotting that allows for flexibility around such a plot.

This is what I have so far:

import networkx as nx
from nxpd import draw
G = nx.MultiDiGraph(with_labels=False,rankdir='LR')
G.graph['dpi'] = 120
G.add_node(1,label=r"$\sigma_1$",shape = 'circle')
G.add_node(2,label=r"$\sigma_2$",shape = 'circle')
G.add_edge(1,1,headport='nw',tailport='sw',label='sp')
G.add_edge(2,2,headport='se',tailport='ne')
G.add_edge(1,2,label='1-sp')
draw(G, show='ipynb')

Which obviously is a far cry from what I'm trying to do.

Several things are not working:

  • The headport='nw',tailport='sw' is doing strange things
  • Obviously, r"$\sigma_2$" is not outputting the desired tex based symbols.

How can I plot a graph that looks like the above?

Trying to achieve the same with matplotlib and networkx

import matplotlib.pyplot as plt
import networkx as nx

%matplotlib inline

G=nx.MultiDiGraph()
pos = {0:(100,100),1:(0,0)}
G.add_node(0)
G.add_node(1)
G.add_edge(0,0)
G.add_edge(0,1)
G.add_edge(1,0)

labels={}
labels[0]=r'$\sigma_1$'
labels[1]=r'$\sigma_2$'
nx.draw_networkx_labels(G,pos,labels)
plt.axis('off')
plt.show() # display

Which, for some reason, outputs a blank screen?

Community
  • 1
  • 1
tmo
  • 1,393
  • 1
  • 17
  • 47

1 Answers1

1

I'm not familiar with nxpd (and import nxpd doesn't work for me, so I don't have it on my computer), so I'm using the nx commands.

import networkx as nx
G = nx.MultiDiGraph(with_labels=False,rankdir='LR')
G.graph['dpi'] = 120
G.add_node(1,label=r"$\sigma_1$",shape = 'circle')
G.add_node(2,label=r"$\sigma_2$",shape = 'circle')
G.add_edge(1,1,headport='nw',tailport='sw',label='sp')
G.add_edge(2,2,headport='se',tailport='ne')
G.add_edge(1,2,label='1-sp')
labels = {node:G.node[node]['label'] for node in G.nodes()}
nx.draw(G, with_labels=True, labels=labels)

import pylab
pylab.savefig('tmp.png')

enter image description here

(note --- the self edge isn't appearing here, but I think you know how to fix that.)


I think the reason that your final block of code outputs a blank screen is because of this known bug (not networkx's fault) pylab/networkx; no node labels displayed after update. If you save the figure the labels will be there.

Community
  • 1
  • 1
Joel
  • 22,598
  • 6
  • 69
  • 93
  • 1
    Hiyas, and thanks for the input. I haven't tagged it as an answer, as the code you've provided is a far cry from the figure I'm trying to imitate (see top). – tmo Jan 05 '16 at 17:37