首页 > 解决方案 > PyDot node label with subscript

问题描述

I have the attached code to great a pydot graph and I would like one of the labels to have subscript. I tried the following but it just renders it as Y<SUB>2</SUB>. How can I obtain it with subscript?

import pydot

graph = pydot.Dot(graph_type='digraph', rankdir="LR")
# add node
graph.add_node(pydot.Node('X', label='X'))
graph.add_node(pydot.Node('Y', label='Y<SUB>2</SUB>'))

# add edege
graph.add_edge(pydot.Edge('X', 'Y'))
graph.write_png("mygraph.png")

标签: pythonpydot

解决方案


如果您在您的环境中设置了 LaTex,您可以使用 LaTex 标记为您的图形执行此操作。您必须通过 LaTex 传递结果图,因此需要 dot2tex 工具https://dot2tex.readthedocs.io/en/latest/

graph.add_node(pydot.Node('Y', label='$Y_{2}$'))

推荐阅读