[Python] Networkx (or Graphviz) rotate node labels clockwise around the center of the plot

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a similar plot drawn with Networkx/Graphviz:

    import networkx as nx
    from networkx.drawing.nx_agraph import graphviz_layout

    T = nx.balanced_tree(2, 5)
    pos = graphviz_layout(T, prog="twopi")
    nx.draw_networkx(T, pos, with_labels=True)
    plt.show()


    Which gives me the following plot:

    [​IMG]

    What I want:

    I want that all the node labels are rotated clockwise around the center of the graph. How do i do this? I would prefer a way directly with Networkx or Graphviz.

    With "rotated clockwise" I mean similar like the labels on this polar plot:

    [​IMG]

    I also tried with the following code:

    T = nx.balanced_tree(2, 5)
    pos = graphviz_layout(T, prog="twopi")
    nx.draw_networkx(T, pos, with_labels=False)

    text = nx.draw_networkx_labels(T, pos=pos)
    for _, t in text.items():
    t.set_rotation('clockwise')

    plt.show()


    But for set_rotation() only 'horizontal', 'vertical', numeric value, or None are supported.

    I also found similar questions, but non of which could help me:

    Does not plot the node labels and it's a quiet old question from 2017, also it seems for me to complicated: NetworkX node labels relative position

    Answer is that it is not possible with graphviz in 2019: https://stackoverflow.com/questions/55009159/change-label-orientation-90º-of-a-node-in-graphviz

    Continue reading...

Compartilhe esta Página