def create_gif(filename): anim = animation(fig, animate, init_func=create_legend, frames=duration * fps, interval=fps) anim.save(f'animations/{filename}/example.gif')
def __init__(self,init,maskBool,saniBool,SDbool): #init = [N,inf,rec] self.N = init[0] suc = self.N-init[1]-(init[2]+init[3]) self.cols = np.repeat([sus_col,inf_col,rem_col,ded_col], [suc,init[1],init[2],init[3]]) self.low,self.high = -stage_dimension/2,stage_dimension/2 self.pos = np.random.uniform(self.low,self.high,size=(2,self.N)) self.stream = self.data_stream() self.fig,self.ax = plt.subplots(); self.ani = animation(self.fig, self.update, interval=80, init_func=self.setup, blit=True)
def animate(data, impl, frames, plot_size=13000): fig, ax = plt.subplots() lines, = plt.plot(0, 0, '.') plt.axis([-plot_size, plot_size, -plot_size, plot_size]) _ = animation(fig=fig, func=lambda step: impl(data, step, lines), init_func=lambda: None, frames=frames, repeat=False, interval=1) plt.show()
def graphIt(aList): x = 0 listY = [] fig = plt.figure() graph, = plt.plot([], [], 'o') #(listY, aList, 'o') for y in aList: x += 1 listY.append(x) plt.plot(x, y, marker='o', markersize=3) def animate(i): graph.set_data(listY[:i + 1], aList[:i + 1]) return graph plt.xlabel('Number of Steps') plt.ylabel('Value at Step') plt.title('Collatz Conjecture') ani = animation(fig, animate, frames=len(listY), interval=50) ani.save("collatzConjecture2.gif", writer='pillow') plt.show()
def begin(file_name, x_axis, file_path='', selected_columns=None, fresh_rate=100, sample=-1, figsize=(15, 6), sep=';'): fig = plt.figure(figsize=figsize) fig.canvas.set_window_title('Liverplot') def animate(i): df = pd.read_csv(file_path + file_name, sep=sep) # If user is going to plot specifc columns, he'll not pass the x_axis inside # selected_columns again. For this convenience, we shall add to the array columns = selected_columns + \ [x_axis] if selected_columns else df.columns # Tail do whats suppose to do. By default, Liverplot will plot the whole # dataframe, however, it's possible to plot only a sample df = df[columns].tail(sample) # Clear axis for next plot plt.cla() for column in df.columns: if column != x_axis: plt.plot(df[x_axis], df[column], label=column) plt.legend(loc='upper left') plt.tight_layout() a = animation(fig, animate, interval=fresh_rate) plt.show()
def multi_animator(self): curves = list() x_temp = list() ax = list() an = list() row, col, fig = self.dynamicCanvas() for n, [x, y] in enumerate(self.funcOrJustDataTargetorAddParam): x_temp.append(np.arange(min(x), max(x), 0.05).reshape(-1, 1)) # TODO subtitle, label names, range limits, legends ax.append(plt.subplot(row, col, n + 1)) if self.funcParams['plot' + str(n + 1)].get('name', None): ax[n].set_xlabel(self.funcParams['plot' + str(n + 1)].get( 'name', None)) plt.plot(x, y, 'bo', markersize=3) curves.append( plt.plot(x_temp[n], self.funcParams['plot' + str(n + 1)]['func'][0]( x_temp[n]), 'r', linewidth=2)) if len(self.funcParams['plot' + str(n + 1)]) > 1: paramStr = '' for key in self.funcParams['plot' + str(n + 1)]: if key != 'func': paramStr += '{}:{}\n'.format( key, self.funcParams['plot' + str(n + 1)][key][0]) an.append(ax[n].annotate(paramStr, xy=(0.75, 0.9), xycoords='axes fraction', fontsize=10, bbox=dict(facecolor='white', alpha=0.8), horizontalalignment='left', verticalalignment='bottom')) else: an.append(None) def update(i): label = 'Time Step {0}'.format(i) plt.suptitle(label) for n, curve in enumerate(curves): curve[0].set_ydata( self.funcParams['plot' + str(n + 1)]['func'][i](x_temp[n])) if len(self.funcParams['plot' + str(n + 1)]) > 1: an[n].remove() paramStr = '' for key in self.funcParams['plot' + str(n + 1)]: if key != 'func' and key != 'name': paramStr += '{}:{}\n'.format( key, self.funcParams['plot' + str(n + 1)][key][i]) #TODO shorten to 5 or 6 digits long an[n] = ax[n].annotate(paramStr, xy=(0.75, 0.9), xycoords='axes fraction', fontsize=10, bbox=dict(facecolor='white', alpha=0.8), horizontalalignment='left', verticalalignment='bottom') if Plotter.isIPy: plt.rcParams["animation.html"] = "jshtml" anim = animation(fig, update, frames=range(len(self.funcParams['plot1']['func'])), interval=self.frameInterval) if Plotter.isIPy: self.anim = anim else: self.anim = anim plt.show()
import random from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation as animation from itertools import count x = [] y = [] index = count() def animate(i): x.append(next(index)) y.append(random.randint(0, 100)) plt.cla() plt.plot(x, y) ani = animation(plt.gcf(), animate, interval=1000) plt.xlabel = 'x' plt.xlabel = 'y' plt.tight_layout() plt.show()
ax2.clear() ax2.axhline( 3.14159265359, color='r', ) ax2.plot(approxdata, '-b') #ax2.plot([it],[approximation], '-o', color = 'red') plt.title(' n = ' + str(n) + ' PI = {0:.16f}'.format(approximation)) # print( str(it) + ". n = " + str(n) + " PI = " + str(approximation)) # print(approximation) # print('\n') it += 1 def animate(i): framex, framey = [], [] for i in range(0, 10000): x = rand.random() y = rand.random() framex.append(x) framey.append(y) ax1.scatter(framex, framey, marker='.') xdata.extend(framex) ydata.extend(framey) approx_pi() ani = animation(fig, animate, interval=1, frames=500, repeat=False) plt.show()
for run in split_at_value(new_data, 'New_Run'): Runs.append(run) Current_run = Runs[-1] container = Current_run[-1]['Container'] lats = [] longs = [] for row in Current_run: lat = float(row['Latitude']) long = float(row['Longitude']) lats.append(lat) longs.append(long) if new_data != old_data: print('Updating ...') plt.cla() if container != 'None': m.warpimage(f"Images/{container}_sheet.png") m.plot(longs, lats, latlon=True, color='c', marker='.', markersize=9, markeredgecolor='k', markeredgewidth=0.5, linestyle='-', linewidth=0.8, alpha=0.75) old_data = new_data ani = animation(fig, animate, interval=1000) plt.show()