【问题标题】:Is it possible to make change the node size according to a list?是否可以根据列表更改节点大小?
【发布时间】:2021-12-11 03:16:13
【问题描述】:

我做了一个图表。在这里,我使用单个值来表示节点大小。我想根据size_of_node列表改变节点大小。

代码:

import matplotlib.pyplot as plt
import networkx as nx 
from netgraph import Graph 

triangle = nx.DiGraph([('a', 'b'), ('a', 'c'), ('b', 'a')])
size_of_node = [3,1, 4]
edge_labels = {
    ('a', 'b') : 3,
    ('a', 'c') : 10,
    ('b', 'a') : 4
}

fig, (ax1) = plt.subplots(1, figsize=(14,14))

Graph(triangle, node_labels=True, edge_labels=edge_labels,
      edge_label_fontdict=dict(size=12, fontweight='bold'),
      edge_layout='curved',
      node_size=3, edge_width=4, arrows=True, ax=ax1)

plt.show()

如何根据列表更改节点大小?

【问题讨论】:

  • 到目前为止你的尝试是什么?

标签: python matplotlib graph


【解决方案1】:

为此,您可以将包含所需节点大小的字典(而不是列表)传递给函数的 node_size 参数。 代码如下:

import matplotlib.pyplot as plt
import networkx as nx 
from netgraph import Graph 

triangle = nx.DiGraph([('a', 'b'), ('a', 'c'), ('b', 'a')])
size_of_node_dict = {'a':3,'b':1, 'c':4}
edge_labels = {
    ('a', 'b') : 3,
    ('a', 'c') : 10,
    ('b', 'a') : 4
}

fig, (ax1) = plt.subplots(1, figsize=(14,14))

Graph(triangle, node_labels=True, edge_labels=edge_labels,
      edge_label_fontdict=dict(size=12, fontweight='bold'),
      edge_layout='curved',    
      node_label_fontdict=dict(size=20), 
      node_size=size_of_node_dict, edge_width=4, arrows=True, ax=ax1)

plt.show()

输出如下:

【讨论】:

    猜你喜欢
    • 2018-04-17
    • 1970-01-01
    • 2017-11-06
    • 2013-04-23
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2017-09-13
    相关资源
    最近更新 更多