Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Graphs: Latex Math rendering of node labels

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

like image 595
IssamLaradji Avatar asked Mar 06 '16 18:03

IssamLaradji


1 Answers

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

import dot2tex
texcode = dot2tex.dot2tex(A.to_string(), format='tikz', crop=True)
like image 145
Aric Avatar answered Sep 30 '22 06:09

Aric