【发布时间】:2020-12-22 16:53:43
【问题描述】:
我正在尝试使用 matplotlib 的 FuncAnimation 类绘制动画图。我想在每一帧绘制具有不同温度值T 的 fermi() 函数。但是情节给出了一个空白图表,为什么?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# Fermi-Dirac Distribution
def fermi(E: float, E_f: float, T: float) -> float:
k_b = 8.617 * (10**-5) # eV/K
return 1/(np.exp((E - E_f)/(k_b * T)) + 1)
fig, ax = plt.subplots()
# Temperature values
T = np.linspace(100, 1000, 10)
# Get colors from coolwarm colormap
colors = plt.get_cmap('coolwarm', 10)
# 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)
# 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 = FuncAnimation(fig=fig, func=animate, frames=range(len(T)), interval=500, repeat=True)
plt.show()
【问题讨论】:
标签: python matplotlib