【问题标题】:Extra Figure in Matplotlib Animation using Smopy使用 Smopy 的 Matplotlib 动画中的额外图形
【发布时间】:2018-02-18 01:03:25
【问题描述】:

我正在尝试在 jupyter 中使用 smopy 和 matplotlib 创建动画地图,但是当我运行代码时,我得到两个数字而不是一个。第一个图显示在地图上方并且为空。谁能告诉我如何解决这个问题,以便只绘制动画?

import smopy
import matplotlib.animation as animation

n= 1000
%matplotlib notebook

def update(curr):
    if curr == n-100:
        a.event_source.stop()
    lons = crime_df.X[curr:curr+100]
    lats = crime_df.Y[curr:curr+100]
    x,y = map.to_pixels(lats,lons)
    ax.scatter(x, y, c='r', alpha=0.7, s=200)
    plt.title(curr)

fig = plt.figure()
ax = smopy.Map((37.6624,-122.5168,37.8231,-122.3589), z=12)
ax = ax.show_mpl(figsize=(8,8))
a = animation.FuncAnimation(fig, update, interval=100)

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    您不应该创建一个额外的数字,如果这是不希望的:忽略plt.figure()

    import smopy
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    n= 1000
    %matplotlib notebook
    
    def update(curr):
        if curr == n-100:
            a.event_source.stop()
        lons = crime_df.X[curr:curr+100]
        lats = crime_df.Y[curr:curr+100]
        x,y = map.to_pixels(lats,lons)
        ax.scatter(x, y, c='r', alpha=0.7, s=200)
        plt.title(curr)
    
    
    m = smopy.Map((37.6624,-122.5168,37.8231,-122.3589), z=12)
    ax = m.show_mpl(figsize=(8,8))
    a = animation.FuncAnimation(ax.figure, update, interval=100)
    

    或者预先创建图形,

    fig, ax = plt.subplots(figsize=(8,8))
    m = smopy.Map((37.6624,-122.5168,37.8231,-122.3589), z=12)
    m.show_mpl(ax = ax)
    a = animation.FuncAnimation(fig, update, interval=100)
    

    【讨论】:

    • 谢谢!那行得通。我之前尝试过 FuncAnimation(ax, update) 但这导致了错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多