Browse Source

add graph plot+

jezv 6 months ago
parent
commit
0d442a84c8
3 changed files with 29 additions and 14 deletions
  1. 2 0
      .gitignore
  2. 22 14
      graph.py
  3. 5 0
      verticle.py

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+__*
+_*

+ 22 - 14
graph.py

@@ -119,6 +119,8 @@ class Graph:
         '''
         x,y - смещение построения графа, если координаты вершин не заданы
         '''
+        fig, ax = plt.subplots()
+
         if not isinstance(self.verticles[0], verticle):
             print("Перепись " + "="*32)
             space = np.linspace(0, 2*np.pi, len(self.verticles)+1)[:-1]
@@ -131,15 +133,21 @@ class Graph:
         for i,row in enumerate(self.matrix):
             for j,elem in enumerate(row):
                 if elem == 0: continue
-                print(i, j ,elem)
-                print(self.verticles[i].x, self.verticles[i].y, self.verticles[j].x, self.verticles[j].y)
-                plt.arrow(self.verticles[i].x, self.verticles[i].y, 
-                          (self.verticles[j].x-self.verticles[i].x)*0.93, (self.verticles[j].y-self.verticles[i].y)*0.93, 
-                          head_width=0.035, head_length=0.035, arrowstyle='->')
-
-        print(self.verticles)
-        x = [v.x for v in self.verticles]
-        y = [v.y for v in self.verticles]
+                arrow = FancyArrowPatch((self.verticles[i].x, self.verticles[i].y),
+                                             (self.verticles[j].x, self.verticles[j].y),
+                                             connectionstyle="arc3,rad=.05", arrowstyle='->', mutation_scale=30)
+                ax.add_patch(arrow)
+                va = 'bottom' if j >= i else 'top'
+                aligment = 'left' if j >= i else 'right'
+                ax.annotate(str(elem), (.5, .5), xycoords=arrow, ha='center', 
+                            va=va, horizontalalignment=aligment, size=20)
+
+        x = []
+        y = []
+        for v in self.verticles:
+            x.append(v.x)
+            y.append(v.y)
+            v.plot()
         plt.scatter(x,y, c='red', zorder=1)
 
 
@@ -179,11 +187,11 @@ if __name__ == "__main__":
     # print("Invert G1:",~g)
     # g.plot(1, 1)
     # h.plot(3.2, 1)
-    g = Graph.build([[0, 1, 1, 1, 1],
-               [1, 0, 1, 1, 1],
-               [1, 1, 0, 1, 1],
-               [1, 1, 1, 0, 1],
-               [1, 1, 1, 1, 0]])
+    g = Graph.build([[0, 1, 0, 1, 1],
+                     [1, 0, 0, 0, 1],
+                     [1, 0, 0, 0, 0],
+                     [1, 0, 1, 0, 0],
+                     [1, 0, 1, 0, 0]])
     g.plot()
     print(g)
     plt.show()

+ 5 - 0
verticle.py

@@ -1,3 +1,5 @@
+import matplotlib.pyplot as plt
+
 class verticle:
     def __init__(self, name, x, y):
         self.name = name
@@ -10,6 +12,9 @@ class verticle:
     def __repr__(self):
         return f"{self.name} ({self.x}, {self.y})"
 
+    def plot(self):
+        plt.annotate(self.name, (self.x, self.y), size=20, bbox=dict(boxstyle="circle,pad=0.2"))
+
 if __name__ == "__main__":
     p1 = verticle("A", 3, 4)
     print(p1)