如何基于某些非唯一属性为网络中的节点着色?

时间:2019-03-05 07:43:02

标签: python matplotlib graph data-visualization networkx

说我在数据框中有以下数据:

Comment列包含用户的评论;在Topic栏中,我在评论中提取了讨论的主要主题;在Opinion列中,我根据用户评论提取了关于该主题的观点; Sentiment列包含用户评论的观点; Topic Count列包含主题在数据中出现的次数; Opinion Count列包含意见在数据中出现的时间。

import pandas as pd

dataset = pd.DataFrame(columns=['Comment', 'Topic', 'Opinion', 'Sentiment', 'Topic Count', 'Opinion Count'])
dataset.Comment = ['xyz', 'wvx', 'uvw', 'rpo', 'tyr', 'rgy', 'tyr', 'opi', 'vmm', 'erw']
dataset.Topic = ['service', 'service', 'payment', 'payment', 'payment', 'service', 'service', 'payment', 'service', 'service']
dataset.Opinion = ['ok', 'ok', 'easy', 'difficult', 'inconvenient', 'inconvenient', 'good', 'good', 'ok', 'ok']
dataset.Sentiment = ['good', 'neutral', 'good', 'bad', 'bad', 'neutral', 'neutral', 'good', 'neutral', 'good']
dataset['Topic Count'] = [6, 6, 4, 4, 4, 6, 6, 4, 6, 6]
dataset['Opinion Count'] = [4, 4, 1, 1, 2, 2, 2, 2, 4, 4]

现在,我想为主题service制作网络图-即,一个图形,它将关于主题service持有的所有观点联系起来。我可以这样:

import networkx as nx
import matplotlib.pyplot as plt
from collections import Counter as C

df = dataset[dataset['Topic']=='service']
df = df.reset_index()

df_temp = pd.DataFrame(columns=['Word', 'Count'])
df_temp.Word = C(df.Opinion).keys()
df_temp.Count = C(df.Opinion).values()

for i in range(len(df)):
    for j in range(len(df_temp)):
        if df['Opinion'][i] == df_temp['Word'][j]:
            df['Opinion Count'][i] = df_temp['Count'][j]

G = nx.Graph()
for i in range(len(df)):
    G.add_edges_from([(df['Topic'][i], df['Opinion'][i])])

pos = nx.spring_layout(G)

dmin = 1
ncenter = 0
for n in pos:
    x, y = pos[n]
    d = (x - 0.5)**2 + (y - 0.5)**2
    if d < dmin:
        ncenter = n
        dmin = d

p = dict(nx.single_source_shortest_path_length(G, ncenter))

plt.figure(figsize=(5, 5))
nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
nx.draw_networkx_nodes(G, pos, nodelist=list(p.keys()),
                       node_size=100*df['Opinion Count'],
#                        node_color=df['Colors']
                      )
nx.draw_networkx_labels(G, pos, with_labels=True, font_size=16)

plt.axis('off')
plt.title('Word Association Network of "service"', fontsize=25)
plt.show()

enter image description here

但是,现在,我想按情感为网络中的每个节点着色。我想用badred着色,用neutral着色yellow,用good着色green。我可以通过在node_color=df['Colors']中添加名为nx.draw_networkx_nodes的列之后在函数Colors中添加类似dataset的参数来实现。 但是,如果不能保证意见具有独特的情感,我该怎么办?

要解释我的意思,请以serviceopinion的形式对主题ok进行评论:

df[df.Opinion=='ok']

enter image description here

我们可以看到ok中有service的4个实例,但是其中2个是中立的,2个是良好的。那么,节点ok应该是什么颜色?如果应该是x%黄色,y%绿色和z%红色,其中xyz是不良,中性的百分比和良好的情感,分别针对给定主题中的给定意见,那么我该如何提出具有这种成分的合适颜色?

0 个答案:

没有答案