Regrouper des codes pythons dans un programme final
Bonjour a tous,
Je suis novice en python alors pardonner mes erreurs et mon manque de savoir
J'ai créer un premier programme qui est une sorte de grille avec des cases colorées
J'ai créer un deuxième programme qui trace une droite avec une animation (avec une classe)
Et je voudrais créer un troisième dans lequel la droite traverserais la grille du deuxième programme.
J'ai essayé les fonctions import mais ça lance deux graphique différents et non superposés
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| """ 1er programme avec case colorées"""
import numpy as np
import matplotlib.pyplot as plt
class Environment:
def __init__(self):
self.m = np.random.randint(2, size=(25,25))
cluster_1 = np.array([[10, 10, 10, 10 ], [1, 1, 1,1]])
self.c = cluster_1
for i in range(self.c.shape[1]):
for j in range(self.c.shape[1]):
if self.m[self.c[0,i]-1,self.c[1,j]-1]:
self.m[self.c[0,i]-1,self.c[1,j]-1] = 1
print(self.m)
plt.matshow(self.m, cmap='jet', interpolation='nearest')
matrice = Environment()
plt.show() |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| """droite diagonale animation"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import matplotlib.animation as animation
class SubplotAnimation(animation.TimedAnimation):
def __init__(self):
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
self.x = np.linspace(0, 200, 256)
self.y = np.linspace(0, 200, 256)
self.line = Line2D([], [], color='black')
self.robot = Line2D(
[], [], color='red', marker='v', markeredgecolor='r')
ax1.add_line(self.line)
ax1.add_line(self.robot)
ax1.set_xlim(0, 100)
ax1.set_ylim(0, 100)
plt.plot([10, ], [10,],'sk')
animation.TimedAnimation.__init__(self, fig, interval=100, blit=True)
def _draw_frame(self, framedata):
i = framedata
head = i - 1
self.line.set_data(self.x[:i], self.y[:i])
self.robot.set_data(self.x[head], self.y[head])
def new_frame_seq(self):
return iter(range(self.x.size))
ani = SubplotAnimation()
plt.show() |