用这样的颜色边缘进行二分表示怎么样?
以下是生成图像的代码。
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()
得到像你在 yEd 中画的东西
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()