In [14]:
prefixes = """PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sem: <http://semanticweb.cs.vu.nl/2009/11/sem/>
PREFIX nwr: <http://www.newsreader-project.eu/>"""

endpoint = "http://hlt-services7.fbk.eu:8890/sparql"

In [15]:
import rdflib
import itertools
import time
from SPARQLWrapper import SPARQLWrapper2, JSON

sparql = SPARQLWrapper2(endpoint)
sparql.setQuery(prefixes + """
SELECT  DISTINCT ?actor 
WHERE {
      ?entity sem:hasActor ?actor .
} 
limit 10
""")
#sparql.setReturnFormat(JSON)
ret = sparql.query()
print ret.variables
for binding in ret.bindings :
        # each binding is a dictionary. Let us just print the results
        print binding['actor'].value


[u'actor']
http://dbpedia.org/resource/Ford_Motor_Company
http://dbpedia.org/resource/CBU_(AM)
http://dbpedia.org/resource/Land_Rover
http://dbpedia.org/resource/Volkswagen
http://dbpedia.org/resource/Chrysler
http://dbpedia.org/resource/Japan
http://dbpedia.org/resource/Durango
http://dbpedia.org/resource/Judge
http://dbpedia.org/resource/Lexus
http://dbpedia.org/resource/Mini

In [4]:
sparql.setQuery(prefixes + """
SELECT ?org (count(?org) as ?numOrg)
WHERE {
      ?org a nwr:person .
      ?org a <http://dbpedia.org/ontology/Person> .
      ?org rdfs:label ?orgLabel .
} 
GROUP BY ?org
ORDER BY DESC(?numOrg)
LIMIT 100
""")
#sparql.setReturnFormat(JSON)
ret = sparql.query()
print ret.variables
plot = []
dlen = len("http://dbpedia.org/resource")
print dlen
for binding in ret.bindings :
        # each binding is a dictionary. Let us just print the results
        org = binding['org'].value
        num = binding['numOrg'].value
        org_sm = org[dlen+1:len(org)]
        plot.append((org_sm, int(num)))
print plot


[u'org', u'numOrg']
27
[(u'Abraham_Lincoln', 115), (u'William_Clay_Ford,_Jr.', 89), (u'Henry_Ford', 72), (u'Nissan_Leaf', 51), (u'List_of_characters_in_Heroes', 51), (u'Scuderia_Ferrari', 51), (u'Carlos_Ghosn', 49), (u'Mark_Fields_(businessman)', 47), (u'Team_Lotus', 47), (u'Alan_Mulally', 36), (u'Michael_Schumacher', 36), (u'Sergio_Marchionne', 34), (u'Lewis_Hamilton', 33), (u'Mary_(mother_of_Jesus)', 32), (u'Jim_Press', 32), (u'James_Farley', 32), (u'Lewis_Booth', 31), (u'Jacques_Nasser', 30), (u'James_Bond', 30), (u'Ferdinand_Porsche', 30), (u'Ferdinand_Pi\xebch', 29), (u'Barack_Obama', 29), (u'Oscar_De_La_Hoya', 29), (u'Fernando_Alonso', 29), (u'George_W._Bush', 29), (u'Robert_Rubin', 28), (u'Li_Shufu', 28), (u'Max_Born', 28), (u'Robert_Nardelli', 27), (u'Penske_Racing', 27), (u'Martin_Winterkorn', 27), (u'Juan_Pablo_Montoya', 27), (u'Mahathir_Mohamad', 26), (u'Ratan_Tata', 26), (u'Edsel_Ford', 26), (u'Saint', 26), (u'Wolfgang_Bernhard', 25), (u'Tony_Stewart', 25), (u'Rick_Wagoner', 25), (u'David_Richards_(racing)', 25), (u'Daewoo_Matiz', 25), (u'Dieter_Zetsche', 23), (u'Ross_Brawn', 23), (u'Wendelin_Wiedeking', 23), (u'Walter_Chrysler', 23), (u'Roger_Penske', 23), (u'Peter_Horbury', 22), (u'Jenson_Button', 22), (u'Victoria_Beckham', 22), (u'Edge_(wrestler)', 22), (u'Bob_Lutz_(businessman)', 22), (u'Angela_Merkel', 22), (u'Kirk_Kerkorian', 22), (u'Ralf_Schumacher', 22), (u'David_Smith_(sculptor)', 21), (u'Mark_Webber', 21), (u'Ulrich_Bez', 21), (u'Jim_Padilla', 21), (u'N\xfcrburgring', 21), (u'John_Towers_(businessman)', 21), (u'Daniel_Craig', 21), (u'Rama', 21), (u'Kyle_Busch', 21), (u'Mark_Martin', 21), (u'Scuderia_Toro_Rosso', 21), (u'Tony_Fernandes', 21), (u'The_Notorious_B.I.G.', 21), (u'Gerry_McGovern', 20), (u'Gordon_Brown', 20), (u'Osamu_Suzuki', 20), (u'Brawn_GP', 20), (u'Giancarlo_Fisichella', 20), (u'J\xfcrgen_E._Schrempp', 19), (u'Scott_Speed', 19), (u'Matthew_Taylor_(footballer)', 19), (u'Gerald_Ford', 19), (u'Enzo_Ferrari', 19), (u'Gospel_of_Mark', 19), (u'John_F._Kennedy', 19), (u'Kimi_R\xe4ikk\xf6nen', 19), (u'David_Coulthard', 19), (u'Peter_Mandelson', 19), (u'Jeff_Gordon', 19), (u'Saint_Peter', 19), (u'Abdullah_Ahmad_Badawi', 19), (u'Bernd_Pischetsrieder', 19), (u'William_C._Durant', 19), (u'Jimmie_Johnson', 18), (u'Frederick_Henderson', 18), (u'Sebastian_Vettel', 18), (u'Peter_Forster', 18), (u'Ferdinand_Alexander_Porsche', 18), (u'Sean_Combs', 18), (u'List_of_The_Tribe_characters', 18), (u'Chapter_(religion)', 18), (u'Bill_Clinton', 18), (u'Carl_Edwards', 18), (u'Jarno_Trulli', 18), (u'Rubens_Barrichello', 17), (u'Force_India', 17)]

In [12]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpld3 import enable_notebook
enable_notebook()

df = pd.DataFrame(plot, columns=["org","num"])
df.plot(kind='bar')


Out[12]:
<matplotlib.axes.AxesSubplot at 0x1124e5110>
/Users/pgroth/.virtualenvs/ipy/lib/python2.7/site-packages/mpld3/_objects.py:414: UserWarning: Ignoring legend element: <matplotlib.offsetbox.VPacker object at 0x1128c3f90>
  warnings.warn("Ignoring legend element: {0}".format(child))

In [6]:
import networkx as nx
sparql.setQuery(prefixes + """
SELECT ?org ?org2 (count(?evt) as ?numEvt)
WHERE {
   ?evt a sem:Event .
   ?evt sem:hasActor ?org .
   ?org a nwr:organization  .
   ?org a <http://schema.org/Organization>.
   ?evt sem:hasActor ?org2 .
   ?org2 a nwr:organization .
   ?org2 a <http://schema.org/Organization>.
   FILTER(?org != ?org2) .
} 
ORDER BY DESC(?numEvt)
LIMIT 100
""")
#sparql.setReturnFormat(JSON)
ret = sparql.query()
print ret.variables
g=nx.Graph()
dlen = len("http://dbpedia.org/resource")
print dlen

for binding in ret.bindings :
        # each binding is a dictionary. Let us just print the results
        org = binding['org'].value
        org = org[dlen+1:len(org)]
        
        org2 = binding['org2'].value
        org2 = org2[dlen+1:len(org2)]
        #print num
        num = int(binding['numEvt'].value)
        g.add_edge(org, org2, weight=num)
        
        
#print g.nodes()


[u'org', u'org2', u'numEvt']
27

In [7]:
#for line in nx.generate_edgelist(g, data=True):
 #   print(line)

In [11]:
from mpld3 import enable_notebook
enable_notebook()

pos=nx.spring_layout(g, k=10.0, iterations=100, weight='weight')
nx.draw_networkx_nodes(g,pos,node_size=10,node_color='b', alpha=.2)

nx.draw_networkx_edges(g,pos,edge_color='b',alpha=.4)
nx.draw_networkx_labels(g,pos,font_size=10,font_family='sans-serif')

#plt.grid(color='white', linestyle='solid')
plt.axis('off')
plt.show()

#nx.draw_spring(g,node_size=0,edge_color='b',alpha=.2,font_size=10)


This is a graph