networkx digraph matplotlib graph orientation

huangapple go评论42阅读模式
英文:

networkx digraph matplotlib graph orientation

问题

我正在尝试使用Networkx Dograph和matplotlib创建一个图形,这是我的代码:

def draw(self):
    # 创建一个有向图对象
    G = nx.DiGraph()
    # 设置节点颜色
    node_colors = []
    for tx in self.transactions.values():
        if tx.txid == '1':
            node_colors.append('orange')  # 橙色表示 tips
        elif tx.txid == '0' and self.current_batch == 0:
            node_colors.append('grey')  # 灰色表示第一个交易
        elif tx in self.tips:
            node_colors.append('grey')  # 灰色表示 tips
        else:
            node_colors.append('blue')  # 蓝色表示其他节点

    # 获取节点位置和高度
    pos = {}
    heights = {}
    for tx in self.transactions.values():
        if tx.txid == '0':
            pos[tx.txid] = (0, 0)
            heights[tx.txid] = 0
        else:
            max_height = max(heights[parent_txid] for parent_txid in tx.parent_txids)
            pos[tx.txid] = (max_height + 1, len(heights))
            heights[tx.txid] = max_height + 1

    # 向图中添加边
    for tx in self.transactions.values():
        for parent_txid in tx.parent_txids:
            G.add_edge(tx.txid, parent_txid)

    # 绘制图形
    plt.figure(figsize=(10, 5))
    nx.draw(G, pos=pos, with_labels=True, node_color=node_colors, node_size=1000, node_shape='s')
    plt.ylim(-1, len(heights))
    plt.xlim(-1, max(heights.values()) + 1)
    plt.show()

这是您的输出图形。链接版本是我想要的,节点0应该位于最左侧的中间位置,后续节点应该水平增长而不是垂直增长。目前,新节点水平增长,但也垂直增长。我尝试过调整 plt.ylim(-1, len(heights))plt.xlim(-1, max(heights.values()) + 1),但无法解决问题。如果有人能帮我解决这个问题,那将非常好。如果问题不清楚,请告诉我。

英文:

I am trying to create a graph using Networkx Dograph and matplotlib, this is my code

def draw(self):
    # Create a directed graph object
    G = nx.DiGraph()
    # Set node colors
    node_colors = []
    for tx in self.transactions.values():
        #print("batch numer", self.num_batches)
        if tx.txid == '1':
            node_colors.append('orange')  # orange for tips
        elif tx.txid == '0' and self.current_batch == 0:
            node_colors.append('grey')  # gray for first transaction
        elif tx in self.tips:
            node_colors.append('grey')  # gray for tips
        else:
            node_colors.append('blue')  # blue for other nodes

    # Get node positions and heights
    pos = {}
    heights = {}
    for tx in self.transactions.values():
        if tx.txid == '0':
            pos[tx.txid] = (0, 0)
            heights[tx.txid] = 0
        else:
            max_height = max(heights[parent_txid] for parent_txid in tx.parent_txids)
            pos[tx.txid] = (max_height + 1, len(heights))
            heights[tx.txid] = max_height + 1

    # Add edges to the graph
    for tx in self.transactions.values():
        for parent_txid in tx.parent_txids:
            G.add_edge(tx.txid, parent_txid)

    # Draw the graph
    plt.figure(figsize=(10, 5))
    nx.draw(G, pos=pos, with_labels=True, node_color=node_colors, node_size=1000, node_shape='s')
    #plt.axis('equal')
    plt.ylim(-1, len(heights))
    plt.xlim(-1, max(heights.values()) + 1)
    plt.show()

this is the output

networkx digraph matplotlib graph orientation

The inked version is how I want it, node 0 should be in the mid at left most side and later nodes shod grow horizontly but not vertically. curenttly, the new node grow horizontally but also vertically. I have tried to play with
plt.ylim(-1, len(heights)) plt.xlim(-1, max(heights.values()) + 1) but cannot fix it, it will be great if someone can help me fix it. and please let me know if the question is not clear.

答案1

得分: 1

我认为你正在寻找的比你想象的要简单得多:

参考这个帖子。

如果你添加一个简单的

from networkx.drawing.nx_agraph import graphviz_layout

并将你的

nx.draw(G, pos=pos, with_labels=True, node_color=node_colors, node_size=1000, node_shape='s')

更改为

nx.draw(G, graphviz_layout(G, prog='dot', args="-Grankdir=RL"), with_labels=True, node_color=node_colors, node_size=1000, node_shape='s')

你应该就可以了。

我重新创建了你的用例,结果是这个图表(不要介意颜色,我没有花时间调整它以适应你的用例):
networkx digraph matplotlib graph orientation

绘制图形并不是一项简单的任务,尽量在可能的情况下寻找现有算法来为你做布局。

对于未来的问题:

尽量提供一个你问题的最小工作示例。仅从你的问题来看,很难重现你的问题,因为你没有提供“transactions”、“tips”或“current_batch”的确切性质(事实上,最后两个最好不要提供,因为它们并不是问题的一部分)。

希望这有所帮助 networkx digraph matplotlib graph orientation

英文:

I think what you're looking for is much easier than you are making it:

Check out this post for reference.

If you add a simple

from networkx.drawing.nx_agraph import graphviz_layout

and change your

> nx.draw(G, pos=pos, with_labels=True, node_color=node_colors, node_size=1000, node_shape='s')

to

nx.draw(G, graphviz_layout(G, prog='dot', args="-Grankdir=RL"), with_labels=True, node_color=node_colors, node_size=1000, node_shape='s')

you should be good to go.
I recreated your use case and it resulted in this graph (don't mind the coloring, I did not take the time to adjust that to your use case):
networkx digraph matplotlib graph orientation

Drawing graphs is not a trivial task and you should try and look for existing algorithms to do the layout for you whenever possible.
For future questions:

Try to provide a minimal working example of your problem. From your question alone it is not easily possible to recreate your issue because you did not provide the exact nature of "transactions", "tips" or "current_batch" (the 2 last ones would actually be better to leave out since they are not part of the problem).

Hope this helps networkx digraph matplotlib graph orientation

huangapple
  • 本文由 发表于 2023年5月15日 11:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定