import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
df
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", node_shape="s", alpha=0.5, linewidths=40)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
df
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, width=5, edge_color="skyblue", )
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
df
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, font_size=25, font_color="yellow", font_weight="bold")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
df
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", node_shape="o", alpha=0.5, linewidths=4, font_size=25, font_color="grey", font_weight="bold", width=2, edge_color="grey")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A','E','F','E','G','G','D','F'], 'to':['D', 'A', 'E','C','A','F','G','D','B','G','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.fruchterman_reingold_layout(G))
plt.title("fruchterman_reingold")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A','E','F','E','G','G','D','F'], 'to':['D', 'A', 'E','C','A','F','G','D','B','G','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.circular_layout(G))
plt.title("circular")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A','E','F','E','G','G','D','F'], 'to':['D', 'A', 'E','C','A','F','G','D','B','G','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.spring_layout(G))
plt.title("spring")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A','E','F','E','G','G','D','F'], 'to':['D', 'A', 'E','C','A','F','G','D','B','G','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.spectral_layout(G))
plt.title("spectral")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A','E','F','E','G','G','D','F'], 'to':['D', 'A', 'E','C','A','F','G','D','B','G','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.random_layout(G))
plt.title("random")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['D', 'A', 'B', 'C','A'], 'to':['A', 'D', 'A', 'E','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph() )
plt.title("Directed")
nx.draw(G, with_labels=True, node_size=1500, alpha=0.3, arrows=True)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['D', 'A', 'B', 'C','A'], 'to':['A', 'D', 'A', 'E','C']})
G=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.Graph() )
nx.draw(G, with_labels=True, node_size=1500, alpha=0.3, arrows=True)
plt.title("UN-Directed")
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C']})
df
carac = pd.DataFrame({ 'ID':['A', 'B', 'C','D','E'], 'myvalue':['group1','group1','group2','group3','group3'] })
carac
G=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.Graph() )
G.nodes()
carac= carac.set_index('ID')
carac=carac.reindex(G.nodes())
carac['myvalue']=pd.Categorical(carac['myvalue'])
carac['myvalue'].cat.codes
nx.draw(G, with_labels=True, node_color=carac['myvalue'].cat.codes, cmap=plt.cm.Set1, node_size=1500)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C'], 'value':[1, 10, 5, 5]})
df
G=nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.Graph() )
nx.draw(G, with_labels=True, node_color='skyblue', node_size=1500, edge_color=df['value'], width=10.0, edge_cmap=plt.cm.Blues)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
df = pd.DataFrame({ 'from':['A', 'B', 'C','A'], 'to':['D', 'A', 'E','C'], 'value':['typeA', 'typeA', 'typeB', 'typeB']})
df
df['value']=pd.Categorical(df['value'])
df['value'].cat.codes
G=nx.from_pandas_edgelist(df, 'from', 'to', ['value'])
G.nodes()
G.edges()
nx.draw(G, with_labels=True, node_color='skyblue', node_size=1500, edge_color=df['value'].cat.codes, width=10.0, edge_cmap=plt.cm.Set2)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
fig = plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
ind1=[5,10,3,4,8,10,12,1,9,4]
ind5=[1,1,13,4,18,5,2,11,3,8]
df = pd.DataFrame({ 'A':ind1, 'B':ind1 + np.random.randint(10, size=(10)) , 'C':ind1 + np.random.randint(10, size=(10)) , 'D':ind1 + np.random.randint(5, size=(10)) , 'E':ind1 + np.random.randint(5, size=(10)), 'F':ind5, 'G':ind5 + np.random.randint(5, size=(10)) , 'H':ind5 + np.random.randint(5, size=(10)), 'I':ind5 + np.random.randint(5, size=(10)), 'J':ind5 + np.random.randint(5, size=(10))})
corr = df.corr()
links = corr.stack().reset_index()
links.columns = ['var1', 'var2','value']
links_filtered=links.loc[ (links['value'] > 0.8) & (links['var1'] != links['var2']) ]
links_filtered
G=nx.from_pandas_edgelist(links_filtered, 'var1', 'var2')
nx.draw(G, with_labels=True, node_color='orange', node_size=400, edge_color='black', linewidths=1, font_size=15)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
fig = plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
import seaborn as sns
df = sns.load_dataset('iris')
df
corr = df.drop(['species'], axis=1).T.corr()
corr
links = corr.stack().reset_index()
links.columns = ['var1', 'var2','value']
links
links_filtered=links.loc[ (links['value'] > 0.85) & (links['var1'] != links['var2']) ]
links_filtered
G=nx.from_pandas_edgelist(links_filtered, 'var1', 'var2')
nx.draw(G, with_labels=True, node_color='skyblue', node_size=200, edge_color='black', linewidths=1, font_size=5)
plt.show()
import matplotlib.pyplot as plt
import networkx as nx
import random
G = nx.gnp_random_graph(10,0.3)
for u,v,d in G.edges(data=True):
d['weight'] = random.random()
edges,weights = zip(*nx.get_edge_attributes(G,'weight').items())
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color='b', edgelist=edges, edge_color=weights, width=10.0, edge_cmap=plt.cm.Blues)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
a = ['A', 'A', 'A', 'C','B']
b = ['B', 'D', 'C', 'D','D']
weigth= [1, 1, 3, 1, 1]
df = pd.DataFrame({ 'from':a, 'to':b, 'weigth':weigth})
df
G=nx.from_pandas_edgelist(df=df, source='from', target='to', create_using=nx.Graph() )
nx.draw(G, pos=nx.spring_layout(G), arrows=True, with_labels=True, node_size=82, node_color="skyblue", node_shape="o", alpha=0.8, linewidths=13)
plt.show()
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
a = ['A', 'A', 'A', 'C','B']
b = ['B', 'D', 'C', 'D','D']
weigth= [1, 1, 3, 1, 1]
df = pd.DataFrame({ 'from':a, 'to':b, 'weigth':weigth})
df
G=nx.from_pandas_edgelist(df=df, source='from', target='to', edge_attr=['weigth'] )
nx.draw(G, pos=nx.spring_layout(G), arrows=True, with_labels=True, node_size=82, node_color="skyblue", node_shape="o", alpha=0.8, linewidths=13)
plt.show()
The blogger opened a new official account , I hope you can scan the code and pay attention , Thank you very much .
This article is from :https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py