2012-11-28 22 views
5

मैं एक द्वि-पक्षीय सेट समस्या के कनेक्शन मैट्रिक्स को देखने की कोशिश कर रहा हूं। मैं ऐसा कैसे कर सकता हूं जो सर्वोत्तम प्रदर्शन करता है?मैटलप्लिब के साथ कनेक्शन मैट्रिक्स को कैसे देखूं?

मैं इस एक ग्राफिक्स प्रोग्राम yed उपयोग करने के साथ शुरू कर दिया है:

example

हलकों लाल और नीले रंग और वर्गों के बीच संबंध एक और किसी खास प्रकार के वर्णन। लाल और नीले रंग के दोनों वर्गों पर उनके बारे में कुछ प्रकार का पाठ होगा।

हालांकि मैटलप्लिब के साथ इस grafic उत्पन्न करने के लिए यह अच्छा होगा, क्योंकि मैं इसे संलग्न डेटा के साथ फ्लाई पर उत्पन्न करना चाहता हूं। मैं ऐसा कैसे करूं? मेरे डेटा इस तरह थोड़े दिखता है:

डाटा:

name_blue name_red Connection Type 
bluepart1 redpart1 1 
bluepart1 redpart2 1 
bluepart1 redpart3 1 
bluepart3 redpart2 2 
bluepart4 redpart2 2 
... 

और इतने पर। मैं नमस्ते/लाल वर्गों पर नमस्तेग्स लिखना चाहता हूं, ताकि उपयोगकर्ता जानता है कि कौन सा है।

अनुवर्ती प्रश्न: मैं नोड्स के आंशिक रूप से चिह्नित नीले/लाल के साथ इस से ग्राफ कैसे उत्पन्न कर सकता हूं? एक तरह से इस तरह:

graphexample

लेकिन नोड्स उनके द्विपक्षीय प्रकृति को दर्शाती के साथ। मैं अभी भी इस पर अंधेरे में थोड़ा सा हूं, मुख्य रूप से क्योंकि मुझे नहीं पता कि matplotlib से इसका सामना कैसे किया जाए। मैं इस बारे में कुछ अच्छे सुझावों की उम्मीद कर रहा हूं कि इसे कैसे विज़ुअलाइज़ किया जाए और शायद एक उदाहरण कार्यान्वयन जो मुझे रास्ता दिखाता है।

+1

आप एक अलग प्रश्न में अपने अनुवर्ती अप सवाल विभाजित करना चाहिए। – tacaswell

+0

मैंने कोशिश की कि मैं^^ 2 सबप्लॉट्स के ग्रिड में चित्रों को चिपका रहा हूं। हालांकि यह वास्तव में पाइथोनिक या ऐसा करने का matplotlib तरीका नहीं है। मेरे और अन्य लोगों के लिए एक बेहतर समाधान जो इस तरह के विज़ुअलाइजेशन की आवश्यकता है, की सराहना की जाएगी। – tarrasch

उत्तर

0

क्या इस तरह रंग किनारों के साथ एक द्विपक्षीय प्रतिनिधित्व करने के बारे में:

यहाँ एक उदाहरण है?

Bipartite graph with different kinds of connections

के बाद कोड उस छवि उत्पन्न है।

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 में बना रहे हैं की तरह कुछ प्राप्त करने के लिए

Connection matrix

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() 
+0

स्वीकार करेगा, अगर कनेक्शन मैट्रिक्स भी वहां थे ... :) – tarrasch

+0

@tarrasch ओह, मैं समझता हूं। मुझे यकीन नहीं है कि एक साफ ग्राफ प्रतिनिधित्व संभव है, सिवाय इसके कि यदि आप जानते हैं कि सेट छोटे हैं क्योंकि आपको कई क्लिक्स इंटरकनेक्टेड आकर्षित करना होगा। मैंने matplotlib के साथ ठीक किया जो आपने YEd के साथ किया था, जो मुझे लगता है कि इस जानकारी को सबसे अच्छा दिखाता है और कोड – bcurcio

+0

कोड के साथ उत्तर संशोधित करता है, आपने इसे अर्जित किया है :) यदि आप मेरे फ़ाइल प्रारूप को पहचान सकते हैं और इसे परिवर्तित कर सकते हैं तो मैं अतिरिक्त रूप से आपको ऊपर ले जाऊंगा: "जोड़ी: (मैन डेस्क महिला डीईएससी) टाइप: 1" जहां मैन डीईएससी नोड का नाम है। मेरे पास इस तरह की एक पूरी फाइल है। – tarrasch

6

networkx का उपयोग करने का प्रयास करें। आप नोड्स पर विशिष्ट रंगों के साथ ग्राफ को प्रदर्शित करने के लिए इसका उपयोग कर सकते हैं और अपने डेटा से मेल खाने के लिए लिंक का उपयोग कर सकते हैं।

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() 

Simple Example

+0

निश्चित रूप से इसे जांच लेंगे। – tarrasch

5

यहाँ एक और 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() 

enter image description here

है

enter image description here

enter image description here

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^