Python Graphs: Latex Math rendering of node labels

天大地大妈咪最大 提交于 2020-05-15 04:03:29

问题


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


回答1:


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

import dot2tex
texcode = dot2tex.dot2tex(A.to_string(), format='tikz', crop=True)


来源:https://stackoverflow.com/questions/35830447/python-graphs-latex-math-rendering-of-node-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!