main.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import numpy as np
  2. import networkx as nx
  3. import matplotlib.pyplot as plt
  4. class Lattice():
  5. def __init__(self, n):
  6. self.n = n
  7. self.graph = self._generate_lattice(self.n)
  8. def _generate_lattice(self, n):
  9. lx = range(-n ,n+1, 1)
  10. ly = range(-n ,n+1, 1)
  11. lattice = nx.grid_2d_graph(lx, ly)
  12. return lattice
  13. def generate_configuration(self, p):
  14. nb_edges = len(self.graph.edges())
  15. config = np.random.binomial(1, p, size=nb_edges)
  16. return config
  17. def draw(self, config):
  18. _pos = {(x,y):(x,y) for x,y in self.graph.nodes()}
  19. edge_colors = ['white' if config[i]==0 else 'black' for i,e in enumerate(self.graph.edges)]
  20. options = {
  21. 'pos': _pos,
  22. 'width':2,
  23. 'edge_color':edge_colors,
  24. 'node_size': 0
  25. }
  26. fig = plt.figure(figsize=(20,20))
  27. nx.draw(
  28. G=self.graph,
  29. **options
  30. )
  31. plt.savefig('./figures/my_fig.png',dpi=1000)
  32. plt.show()
  33. if __name__ == "__main__":
  34. lattice = Lattice(10)
  35. latt_conf = lattice.generate_configuration(.2)
  36. lattice.draw(latt_conf)