vendredi 1 juin 2018

Adding graph in Python

I'm trying to generate this figure in small world networks simulation

which is looks similar to the figure in the paper SMALL WORLD AND SCALE FREE MODEL OF TRANSMISSION OF SARS available on page 1752 paper link by using this code

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
import random
#defining a function for near neigbours
def near_neighbours(node, N):
    res = []
    x, y = node
    if x !=0: res.append((x-1, y))
    if x !=N: res.append((x+1,y))
    if y !=0: res.append((x,y-1))
    if y !=N: res.append((x,y+1))
    return res

status = {}
for node in G.nodes(): status[node] = 'S'
# Infecting a random node
status[random.choice(G.nodes())] ='I'
#N the size of the lattice
N =10

#=================================================================
p2 = 0.0
p1 = 0.1293
#the value for p1 and p2 can be varied
#=========================================================
r0 = 1/11.2
r1 = 1/17.4
S = N *N-1
E = 0
I = 1
R = 0
s = []
e = []
i = []
r = []
t = 0
time=[]
graphs_in_time =[]

while I > 0:
    t+=1
    newI=0
    newR=0
    newE=0
    for node in G.nodes():
        if status[node] == 'I':
            for nei in G.neighbors(node):
                if status[nei] == 'S':
                    if nei in near_neighbours(node,N):
                        if random.random() < p1:
                            newE+=1
                            status[nei] = 'E'
                    else:
                        if random.random() < p2:
                            newE+=1
                            status[nei] = 'E'
            if random.random() <r1:
                newR+=1
                status[node] = 'R'
        elif status[node] == 'E':
            if random.random() <r0:
                newI+=1
                status[node] = 'I'
    S-=newE
    E+=(newE - newI)
    I+=(newI - newR)
    R+=newR

    if t% 20 == 0:
        print('t=', t, 'S, E, I, R, N =', S, E, I, R, S+E+I+R)
        graphs_in_time.append({k: v for k, v in status.items()})
    s.append(S)
    i.append(I)
    r.append(R)
    e.append(E)
    time.append(t)

But it gives me this output only without the graphs

t= 20 S, E, I, R, N = 84 5 8 3 100
t= 40 S, E, I, R, N = 75 2 9 14 100
t= 60 S, E, I, R, N = 68 5 4 23 100
t= 80 S, E, I, R, N = 57 4 10 29 100
t= 100 S, E, I, R, N = 52 5 8 35 100
t= 120 S, E, I, R, N = 46 4 9 41 100
t= 140 S, E, I, R, N = 37 5 10 48 100
t= 160 S, E, I, R, N = 31 4 5 60 100
t= 180 S, E, I, R, N = 19 4 10 67 100
t= 200 S, E, I, R, N = 5 9 8 78 100
t= 220 S, E, I, R, N = 3 2 11 84 100
t= 240 S, E, I, R, N = 2 1 3 94 100
t= 260 S, E, I, R, N = 2 0 3 95 100

Can somebody help me to finish drawing the graph?




Aucun commentaire:

Enregistrer un commentaire