6

I am using the following code to create a pygraphviz graph. But is it possible to make it render latex math equations (see Figure 1)? If not, is there an alternative python library that plots similar graphs but supports latex rendering ?

import networkx as nx

from networkx.drawing.nx_agraph import to_agraph

G=nx.DiGraph()
G.add_node(1,color='blue',style='filled',
             fillcolor='white',shape='square', label="$3x+2$")
G.add_node(2)
G.add_node(3)
G.add_edge(1, 2)
G.add_edge(1, 3)
G.add_edge(3, 4)

A = to_agraph(G)
A.layout('dot')
A.draw('test1.png')

This results in the following figure

Figure 1

Figure 1

IssamLaradji
  • 6,637
  • 8
  • 43
  • 68
  • I don't see any reference to LaTeX in the [PyGraphviz documentation](https://pygraphviz.github.io/documentation/development/). Is there a reason you think this should work? – ChrisGPT was on strike Mar 06 '16 at 18:11
  • 1
    I see! I edited the question to make the request not restricted to pyGraphviz. In other words, is there a python library that can plot similar graphs with Latex rendering ? I know matplotlib renders latex, but it's not good with such graphs. – IssamLaradji Mar 06 '16 at 18:18
  • Did you managed in the end ? – Tobbey May 05 '20 at 21:15

1 Answers1

4

Maybe https://dot2tex.readthedocs.org/en/latest/ will work for you? Try

import dot2tex
texcode = dot2tex.dot2tex(A.to_string(), format='tikz', crop=True)
Aric
  • 24,511
  • 5
  • 78
  • 77