I am trying to modify the example found here ( https://towardsdatascience.com/intro-to-dynamic-visualization-with-python-animations-and-interactive-plots-f72a7fb69245 ) to run outside of a Jupyter notebook.
The program below produces a *.gif that is animated but not interactive. Can anyone find the error?
#Based on the example here
# https://towardsdatascience.com/intro-to-dynamic-visualization-with-python-animations-and-interactive-plots-f72a7fb69245
# # Import packages
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import numpy.typing as npt
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Slider
# Fermi-Dirac Distribution
def fermi(E: npt.NDArray[np.float64], E_f: float, T: float) -> npt.NDArray[np.float64]:
k_b = 8.617 * (10**-5) # eV/K
return 1/(np.exp((E - E_f)/(k_b * T)) + 1)
# 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))
# Update values
def update(val):
Ef = s_Ef.val
T = s_T.val
f_d.set_data(x, fermi(x, Ef, T))
fig.canvas.draw_idle()
# Create sliders
s_Ef = Slider(ax=ax_Ef, label='Fermi Energy ', valmin=0, valmax=1.0,
valfmt=' %1.1f eV', facecolor='#cc7000')
s_T = Slider(ax=ax_T, label='Temperature ', valmin=100, valmax=1000,
valinit=100, valfmt='%i K', facecolor='#cc7000')
# General plot parameters
mpl.rcParams['font.size'] = 18
mpl.rcParams['axes.linewidth'] = 2
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['xtick.major.size'] = 10
mpl.rcParams['xtick.major.width'] = 2
mpl.rcParams['ytick.major.size'] = 10
mpl.rcParams['ytick.major.width'] = 2
# Temperature values
T = np.linspace(100, 1000, 10)
# Get colors
colors = mpl.colormaps['copper'].resampled(8)
# Create figure and add axes
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)
# Add legend
labels = ['100 K', '200 K', '300 K', '400 K', '500 K', '600 K',
'700 K', '800 K', '900 K', '1000 K']
ax.legend(labels, bbox_to_anchor=(1.05, -0.1), loc='lower left',
frameon=False, labelspacing=0.2)
# 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)
# Create main axis
fig.subplots_adjust(bottom=0.2, top=0.75)
# Create axes for sliders
ax_Ef = fig.add_axes([0.3, 0.85, 0.4, 0.05])
ax_Ef.spines['top'].set_visible(True)
ax_Ef.spines['right'].set_visible(True)
ax_T = fig.add_axes([0.3, 0.92, 0.4, 0.05])
ax_T.spines['top'].set_visible(True)
ax_T.spines['right'].set_visible(True)
# Plot default data
x = np.linspace(0, 1, 100)
Ef_0 = 0.5
T_0 = 100
y = fermi(x, Ef_0, T_0)
f_d, = ax.plot(x, y, linewidth=2.5)
s_Ef.on_changed(update)
s_T.on_changed(update)
# Create animation
ani = FuncAnimation(fig=fig, func=animate, frames=range(len(T)), interval=500, repeat=True)
# Save and show animation
ani.save('AnimatedPlot.gif', writer='pillow', fps=2)
@Wayne pointed out that I need to add an additional package so that the slider bars are interactive. Here is a useful resource
https://towardsdatascience.com/4-python-packages-to-create-interactive-dashboards-d50861d1117e
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.