【问题标题】:animated Plot using FuncAnimation give a blank graph使用 FuncAnimation 的动画绘图给出一个空白图
【发布时间】: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


    【解决方案1】:

    将动画图的轴范围调整为您要使用的数据,然后将创建图。如果您修改该点,将创建动画。由于我的环境是jupyterlab,所以我已经安装了额外的库,所以如果你不需要它们,请删除它们。

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.animation import FuncAnimation
    # Jupyter lab and GIF create
    # from IPython.display import HTML
    # from matplotlib.animation import PillowWriter
    
    # 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 = plt.figure() # update
    ax = plt.axes(xlim=(0, 1), ylim=(0, 10)) # update
    # 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
    anim = FuncAnimation(fig=fig, func=animate, frames=10, interval=500, repeat=True) # range(len(T))
    
    # plt.close()
    # HTML(anim.to_html5_video())
    # anim.save('animate_test.gif', writer='pillow')
    

    【讨论】:

    • 我用你的代码在 jupyter notebook 上试过了,但是得到 RuntimeError: Requested MovieWriter (ffmpeg) not available。有什么想法有什么问题吗?谢谢!
    • 如果你已经安装了ffmpeg并报错,请参考SOsolution
    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多