title: Python matplotlib.animation Jupyter Notebook
body: <p>I use Windows 10 / 64 / Google chrome</p>
  <p>I found a good set-up for animation over Jupyter with the call %matplotlib notebook as here :</p>
  <pre><code>import numpy as np
  import scipy.stats as st
  %matplotlib notebook
  import matplotlib.pyplot as plt
  import matplotlib.animation as animation
  </code></pre>
  <p>For exemple, this one is working pretty well :</p>
  <pre><code>n = 100
  X = st.norm(0,1).rvs(200)
  number_of_frames = np.size(X)
  
  def update_hist(num, second_argument):
      plt.cla()
      plt.hist(X[:num], bins = 20)
      plt.title(&quot;{}&quot;.format(num))
      plt.legend()
  
  fig = plt.figure()
  hist = plt.hist(X)
  
  ani = animation.FuncAnimation(fig, update_hist, number_of_frames, fargs=(X, ), repeat = False )
  plt.show()
  </code></pre>
  <p>But, weirdly the code below doesn't work while it's the same structure, it puzzles me :</p>
  <pre><code>X = np.linspace(-5,5, 150)
  number_of_frames = np.size(X)
  N_max = 100
  N = np.arange(1,N_max+1)
  h = 1/np.sqrt(N)
  
  def update_plot(n, second_argument):
      #plt.cla()
      plt.plot(X, [f(x) for x in X], c = &quot;y&quot;, label = &quot;densité&quot;)
      plt.plot(X, [fen(sample_sort[:n],h[n],x) for x in X], label = &quot;densité&quot;)
      plt.title(&quot;n = {}&quot;.format(n))
  
  fig = plt.figure(6)
  plot = plt.plot(X, [f(x) for x in X], c = &quot;y&quot;, label = &quot;densité&quot;)
      
  ani = animation.FuncAnimation(fig, update_plot, number_of_frames, fargs=(X, ), repeat = False )
  plt.show()
  </code></pre>
  <p>Thanks for your help, best regards.</p>
  <p>EDIT : You don't have the funciton fen(sample_sort[:n],h[n],x) it is a function from float to float taking a x in argument and returning a flot. The argument sample_sort[:n],h[n] it is just maths things I'm trying to understand some statistics anyway, you can remplace with line with what you want np.cos(N[:n]) for exemple.</p>
  <p>EDIT : New code according to the suggestion :</p>
  <pre><code>N_max = 100
  X = np.linspace(-5,5, N_max )
  number_of_frames = np.size(X)
  N = np.arange(1,N_max+1)
  h = 1/np.sqrt(N)
  
  def update_plot(n):
      #plt.cla()
      lines.set_data(X, np.array([fen(sample_sort[:n],h[n],x) for x in X]))
      ax.set_title(&quot;n = {}&quot;.format(n))
      return lines
  
  fig = plt.figure()
  
  ax = plt.axes(xlim=(-4, 4), ylim=(-0.01, 1))
  ax.plot(X, np.array([f(x) for x in X]), 'y-', lw=2, label=&quot;d&quot;)
  lines, = ax.plot([], [], 'b--', lw=3, label=&quot;f&quot;)
  
  ani = animation.FuncAnimation(fig, update_plot, number_of_frames, repeat = False )
  plt.show()
  </code></pre>
  <p>EDIT 2:</p>
  <p>I found a code over internet which does exactly what I would like</p>
  <pre><code># Fermi-Dirac Distribution
  def fermi(E: float, E_f: float, T: float) -&gt; float:
      return 1/(np.exp((E - E_f)/(k_b * T)) + 1)
  
  # Create figure and add axes
  fig = plt.figure(figsize=(6, 4))
  ax = fig.add_subplot(111)
  
  # Get colors from coolwarm colormap
  colors = plt.get_cmap('coolwarm', 10)
  
  # Temperature values
  T = np.array([100*i for i in range(1,11)])
  
  # Create variable reference to plot
  f_d, = ax.plot([], [], linewidth=2.5)
  
  # Add text annotation and create variable reference
  temp = ax.text(1, 1, '', ha='right', va='top', fontsize=24)
  
  # Set axes labels
  ax.set_xlabel('Energy (eV)')
  ax.set_ylabel('Fraction')
  
  # Animation function
  def animate(i):
      x = np.linspace(0, 1, 100)
      y = fermi(x, 0.5, T[i])
      f_d.set_data(x, y)
      f_d.set_color(colors(i))
      temp.set_text(str(int(T[i])) + ' K')
      temp.set_color(colors(i))
  
  # Create animation
  ani = animation.FuncAnimation(fig, animate, frames=range(len(T)), interval=500, repeat=False)
  
  # Ensure the entire plot is visible
  fig.tight_layout()
  
  # show animation
  plt.show()
  </code></pre>