Skip to main content
 首页 » 编程设计

python之使用 Smopy 在 Matplotlib 动画中添加额外图形

2024年09月07日7soundcode

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

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) 

请您参考如下方法:

你不应该创建一个额外的图形,如果它是 undersired:离开 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)