import random import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Generate a plot displaying two elements: # One: Display 6 side die roll results # Two: Plot the order of rolls numRolls = 300 rollTotals = [0, 0, 0, 0, 0, 0] rollSeq = [] # Create a figure with two subplots fig = plt.figure() ax1 = fig.add_subplot(3,2,1) ax2 = fig.add_subplot(3,2,2) ax3 = fig.add_subplot(3,2,3) ax4 = fig.add_subplot(3,2,4) ax5 = fig.add_subplot(3,2,5) ax6 = fig.add_subplot(3,2,6) # Adjust spacing between plots plt.subplots_adjust(top = 0.93, bottom = 0.07, hspace = 0.3) #define the function for use in matplotlib.animation.funcAnimation def animate(i): currentRoll = random.randint(1, 6) rollTotals[currentRoll - 1] += 1 rollSeq.append(currentRoll) # Set subplot data ax1.clear() ax1.bar([1, 2, 3, 4, 5, 6], rollTotals, 1/1.5) ax2.clear() ax2.plot(rollSeq) xlim = len(rollSeq) ax2.set_xlim(xlim - 30, xlim) # Set subplot titles ax1.set_title("Roll Totals") ax2.set_title("Roll Sequence") ani = animation.FuncAnimation(fig, animate, frames=numRolls, interval=50, repeat=False) # Set up formatting for the movie files # Writer = animation.writers['ffmpeg'] # writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) # # Save ani # ani.save(r'D:\Projects\cansat india 2\animation.mp4', writer=writer) plt.show()