由于颜色映射中缺少元素而导致的 ValueError

ValueError due to a missing element in color map

我需要构建一个网络,其中节点(来自 df1)根据来自不同数据集(df2)的标签具有一些特定颜色。在 df1 中并非所有节点都在 df2 中标记为 assigned (例如,因为它们尚未标记,所以它们当前具有 nan 值)。 下面的代码应该很好地说明我的意思:

import networkx as nx
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt, colors as mcolor

# Sample DataFrames
df1 = pd.DataFrame({
    'Node': ['A', 'A', 'B', 'B', 'B', 'Z'],
    'Edge': ['B', 'D', 'N', 'A', 'X', 'C']
})
df2 = pd.DataFrame({
    'Nodes': ['A', 'B', 'C', 'D', 'N', 'S', 'X'],
    'Attribute': [-1, 0, -1.5, 1, 1, 9, 0]
})

# Simplified construction of `colour_map`
uni_val = df2['Attribute'].unique()
colors = plt.cm.jet(np.linspace(0, 1, len(uni_val)))
# Map colours to_hex then zip with
mapper = dict(zip(uni_val, map(mcolor.to_hex, colors)))
color_map =df2.set_index('Nodes')['Attribute'].map(mapper).fillna('black')


G = nx.from_pandas_edgelist(df1, source='Node', target='Edge')
# Add Attribute to each node
nx.set_node_attributes(G, color_map, name="colour")

# Then draw with colours based on attribute values:
nx.draw(G,
        node_color=nx.get_node_attributes(G, 'colour').values(),
        with_labels=True)

plt.show()

Z 不是 df2 因为 df2 是在仅考虑非 NA 值的情况下创建的。 我想将黑色分配给未标记的节点,即那些不在 df2 中的节点。 尝试 运行 上面的代码时,出现此错误:

ValueError: 'c' argument has 7 elements, which is inconsistent with 'x' and 'y' with size 8.

很明显,这个错误是由于缺少颜色添加黑色造成的,不包括在color_map中。 我不清楚的是如何解决这个问题。我希望能得到一些帮助来解决这个问题。

由于 Z 不在 df2 中,而是节点之一,我们应该 reindex the color_map from nodes 节点而不是专门从 df2 创建属性一个 fill_value:

# Create graph before color map:
G = nx.from_pandas_edgelist(df1, source='Node', target='Edge')
# Create Colour map. Ensure all nodes have a value via reindex using nodes
color_map = (
    df2.set_index('Nodes')['Attribute'].map(mapper)
        .reindex(G.nodes(), fill_value='black')
)


color_map 没有重建索引

df2.set_index('Nodes')['Attribute'].map(mapper)

Nodes
A    #000080
B    #0080ff
C    #7dff7a
D    #ff9400
N    #ff9400
S    #800000
X    #0080ff
Name: Attribute, dtype: object

nodes(此处使用节点,因为这将是图表中的所有节点,而不仅仅是 df2 中的节点)

G.nodes()

['A', 'B', 'D', 'N', 'X', 'Z', 'C']

reindex 以确保所有节点都存在于映射中:

df2.set_index('Nodes')['Attribute'].map(mapper).reindex(G.nodes(), fill_value='black')

Nodes
A    #000080
B    #0080ff
D    #ff9400
N    #ff9400
X    #0080ff
Z      black  # <- Missing Nodes are added with specified value
C    #7dff7a
Name: Attribute, dtype: object

完整代码:

import networkx as nx
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt, colors as mcolor

# Sample DataFrames
df1 = pd.DataFrame({
    'Node': ['A', 'A', 'B', 'B', 'B', 'Z'],
    'Edge': ['B', 'D', 'N', 'A', 'X', 'C']
})
df2 = pd.DataFrame({
    'Nodes': ['A', 'B', 'C', 'D', 'N', 'S', 'X'],
    'Attribute': [-1, 0, -1.5, 1, 1, 9, 0]
})

# Simplified construction of `colour_map`
uni_val = df2['Attribute'].unique()
colors = plt.cm.jet(np.linspace(0, 1, len(uni_val)))
# Map colours to_hex then zip with
mapper = dict(zip(uni_val, map(mcolor.to_hex, colors)))

G = nx.from_pandas_edgelist(df1, source='Node', target='Edge')
# Create Colour map. Ensure all nodes have a value via reindex
color_map = (
    df2.set_index('Nodes')['Attribute'].map(mapper)
        .reindex(G.nodes(), fill_value='black')
)
# Add Attribute to each node
nx.set_node_attributes(G, color_map, name="colour")

# Then draw with colours based on attribute values:
nx.draw(G,
        node_color=nx.get_node_attributes(G, 'colour').values(),
        with_labels=True)

plt.show()