Python 如何使用Matplotlib可视化连接矩阵?

Python 如何使用Matplotlib可视化连接矩阵?,python,graph,matplotlib,visualization,Python,Graph,Matplotlib,Visualization,我试图将一个二部集问题的连接矩阵形象化。我如何以最好的方式做到这一点 我已经开始使用图形程序: 圆圈描述了红色和蓝色与另一个正方形之间的某种联系。红色和蓝色方块上都会有一些文字 不过,使用matplotlib生成此图形会更好,因为我希望随附数据动态生成此图形。我该怎么做呢? 我的数据看起来有点像这样: 数据: 等等。我想把名字标签写在蓝色/红色的方块上,这样用户就知道哪个是哪个 跟进问题: 如何从中生成节点部分标记为蓝色/红色的图形?有点像这样: 但是节点反映了它们的二分性。我对此仍有点茫然

我试图将一个二部集问题的连接矩阵形象化。我如何以最好的方式做到这一点

我已经开始使用图形程序:

圆圈描述了红色和蓝色与另一个正方形之间的某种联系。红色和蓝色方块上都会有一些文字

不过,使用matplotlib生成此图形会更好,因为我希望随附数据动态生成此图形。我该怎么做呢? 我的数据看起来有点像这样:

数据:

等等。我想把名字标签写在蓝色/红色的方块上,这样用户就知道哪个是哪个

跟进问题: 如何从中生成节点部分标记为蓝色/红色的图形?有点像这样:

但是节点反映了它们的二分性。我对此仍有点茫然,主要是因为我不知道如何用matplotlib解决这个问题。我希望得到一些关于如何可视化的好建议,也许还有一个示例实现可以为我指明方向

尝试使用。您可以使用它在节点和链接上以特定颜色显示图形,以匹配数据

下面是一个例子:

import itertools
import networkx as nx
import matplotlib.pyplot as plt
edgelist = [(u,v,(u+v)%2) for u,v in itertools.product(range(3),range(3,6))]
G = nx.Graph()
for u,v,t in edgelist:
    G.add_edge(u,v,attr_dict={'t':t})
ecolors = tuple('g' if G[u][v]['t'] == 1 else 'm' for u,v in G.edges())
nx.draw_networkx(G,node_color='rrrccc',edge_color=ecolors)
plt.show()

用这样的颜色边进行二分表示怎么样

下面是生成图像的代码

import matplotlib.pyplot as plt

def addconnection(i,j,c):
  return [((-1,1),(i-1,j-1),c)]

def drawnodes(s,i):
  global ax
  if(i==1):
    color='r'
    posx=1
  else:
    color='b'
    posx=-1

  posy=0
  for n in s:
    plt.gca().add_patch( plt.Circle((posx,posy),radius=0.05,fc=color))
    if posx==1:
      ax.annotate(n,xy=(posx,posy+0.1))
    else:
      ax.annotate(n,xy=(posx-len(n)*0.1,posy+0.1))
    posy+=1

ax=plt.figure().add_subplot(111)
set1=['Man1','Man2','Man3','Man4']
set2=['Woman1','Woman2','Woman3','Woman4','Woman5']
plt.axis([-2,2,-1,max(len(set1),len(set2))+1])
frame=plt.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])

drawnodes(set1,1)
drawnodes(set2,2)

connections=[]
connections+=addconnection(1,2,'g')
connections+=addconnection(1,3,'y')
connections+=addconnection(1,4,'g')
connections+=addconnection(2,1,'g')
connections+=addconnection(4,1,'y')
connections+=addconnection(4,3,'g')
connections+=addconnection(5,4,'y')

for c in connections:
  plt.plot(c[0],c[1],c[2])

plt.show()
得到你正在画的东西

import matplotlib.pyplot as plt

COLOR1='r'
COLOR2='b'

def addconnection(i,j,c):
  if(c==1):
    plt.gca().add_patch( plt.Rectangle((j-0.1,-i-0.1),0.2,0.2,fc='y'))
  if(c==2):
    plt.gca().add_patch( plt.Circle((j,-i),radius=0.1,fc='y'))

def drawnodes(s,i):
  global ax
  if(i==1):
    color=COLOR1
    vx=1
    vy=0
  else:
    color=COLOR2
    vx=0
    vy=1

  step=1
  for n in s:
    posx=step*vx
    posy=step*vy

    plt.gca().add_patch( plt.Circle((posx,-posy),radius=0.1,fc=color))
    ax.annotate(n,xy=(posx-len(n)*0.1,-posy+0.15))
    step+=1

f=open('input.txt')
t=f.readlines()
t=map(lambda x: x.replace('(',' ').replace(')',' ').split(':'),t)

set1=set([])
set2=set([])

for x in t:
  s=x[1].split()
  set1.add(s[0])
  set2.add(s[1])

set1=list(set1)
set2=list(set2)

dic={}
for e in zip(set1,xrange(1,len(set1)+1)): dic[(e[0],1)]=e[1]
for e in zip(set2,xrange(1,len(set2)+1)): dic[(e[0],2)]=e[1]

ax=plt.figure(figsize=(max(len(set1),len(set2))+1,max(len(set1),len(set2))+1)).add_subplot(111)
plt.axis([-1,max(len(set1),len(set2))+1,-max(len(set1),len(set2))-1,1])
frame=plt.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])

drawnodes(set1,1)
drawnodes(set2,2)

for x in t:
  s=x[1].split()
  addconnection(dic[(s[0],1)],dic[(s[1],2)],int(x[2]))

plt.show()


下面是NetworkX/Matplotlib的另一个想法

import random
import networkx as nx
from networkx.algorithms.bipartite import biadjacency_matrix
import matplotlib.pyplot as plt
# generate random bipartite graph, part 1: nodes 0-9, part 2: nodes 10-29
B = nx.bipartite_random_graph(10,20,0.25)
# add some random weights
for u,v in B.edges():
    B[u][v]['weight']=random.randint(0,4)

# spring graphy layout
plt.figure(1)
pos = nx.spring_layout(B)
colors = [d['weight'] for (u,v,d) in B.edges(data=True)]
nx.draw(B,pos,node_color='#A0CBE2',edge_color=colors,width=4,edge_cmap=plt.cm.Blues,with_labels=False)
plt.savefig('one.png')

# simple bipartite layout
plt.figure(2)
pos = {}
for n in range(10):
    pos[n]=(n*2,1)
for n in range(10,30):
    pos[n]=(n-10,0)
nx.draw(B,pos,node_color='#A0CBE2',edge_color=colors,width=4,edge_cmap=plt.cm.Blues,with_labels=False)
plt.savefig('two.png')

# biadjacency matrix colormap
M = biadjacency_matrix(B,row_order=range(10),column_order=range(10,30))
plt.matshow(M,cmap=plt.cm.Blues)
plt.savefig('three.png')
plt.show()


您应该将后续问题拆分为一个单独的问题。我尝试的是将图片粘贴到由n^2个子图组成的网格中。然而,这并不是真正的pythonic或matplotlib方法。如果我和其他需要这种可视化的人有更好的解决方案,我们将不胜感激。我们一定会检查。如果连接矩阵也在其中,我们将接受…:)@塔拉什哦,我明白了。我不确定一个干净的图形表示是可能的,除非你知道集合很小,因为你必须画多个相互关联的团。我用matplotlib绘制的正是你用yEd制作的,我认为它最好地显示了这一信息,并用代码修改了答案OK,你赢得了这个:)如果你能识别我的文件格式并将其转换,我将另外投票给你:“Pair:(MAN DESC WOMAN DESC)type:1”,其中MAN DESC是节点的名称。我有一个这样的完整文件。@tarrasch当然,为了简单起见,我假设您的描述不会包含空格或“:”
import random
import networkx as nx
from networkx.algorithms.bipartite import biadjacency_matrix
import matplotlib.pyplot as plt
# generate random bipartite graph, part 1: nodes 0-9, part 2: nodes 10-29
B = nx.bipartite_random_graph(10,20,0.25)
# add some random weights
for u,v in B.edges():
    B[u][v]['weight']=random.randint(0,4)

# spring graphy layout
plt.figure(1)
pos = nx.spring_layout(B)
colors = [d['weight'] for (u,v,d) in B.edges(data=True)]
nx.draw(B,pos,node_color='#A0CBE2',edge_color=colors,width=4,edge_cmap=plt.cm.Blues,with_labels=False)
plt.savefig('one.png')

# simple bipartite layout
plt.figure(2)
pos = {}
for n in range(10):
    pos[n]=(n*2,1)
for n in range(10,30):
    pos[n]=(n-10,0)
nx.draw(B,pos,node_color='#A0CBE2',edge_color=colors,width=4,edge_cmap=plt.cm.Blues,with_labels=False)
plt.savefig('two.png')

# biadjacency matrix colormap
M = biadjacency_matrix(B,row_order=range(10),column_order=range(10,30))
plt.matshow(M,cmap=plt.cm.Blues)
plt.savefig('three.png')
plt.show()