2017-08-06 55 views
-1

有类似的问题here但我没有相同的问题。下面是我的数据集的快照:Matplotlib动画显示为空

enter image description here

从本质上讲,我想动画随着时间的落客坐标。正如你所看到的日期排序dropoff_datetime。这是我的代码(非常类似于上面的问题)。

fig = plt.figure(figsize=(10,10)) 
ax = plt.axes(xlim=xlim, ylim=ylim) 
points, = ax.plot([], [],'.',alpha = 0.4, markersize = 0.05) 

def init(): 
    points.set_data([], []) 
    return points, 

# animation function. This is called sequentially 
def animate(i): 
    x = test["dropoff_longitude"] 
    y = test["dropoff_latitude"] 
    points.set_data(x, y) 
    return points, 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=100, interval=20, blit=True) 

plt.show() 

与上面链接的问题类似,我的情节只是显示为空。我相信我正确地编码它,不同于上面的链接,我确实看到了坐标在不断变化。我不知道为什么情节是空的。

+1

_I确实看到了那个坐标切换time_:听起来好像是你所期待的。 _我不确定问题是什么:如果你没有描述你的问题,我们也不知道。 – pingul

+0

对不起,我认为我参考上面的链接会清除那个。我更新了我的帖子。即使一切看起来都设置得当,我的情节是空的。 – madsthaks

回答

1

默认一个像素为1点或0.72点(取决于您是在jupyter笔记本中运行代码还是作为独立的绘图)。如果您创建标记大小为0.05的情节,则每个标记的尺寸将分别为0.05像素或0.07像素。由于在屏幕上看到1个像素是非常困难的,特别是如果alpha设置为0.4,所以观察像素的二十分之一是不可能的。

解决方案:设置为markersize = 5或更高。

一个完整的工作示例:

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import pandas as pd 

test = pd.DataFrame({"dropoff_longitude": [-72,-73,-74,-74], 
        "dropoff_latitude": [40,41,42,43]}) 

xlim=(-71,-75) 
ylim=(39,44) 
fig = plt.figure(figsize=(10,10)) 
ax = plt.axes(xlim=xlim, ylim=ylim) 
points, = ax.plot([], [],'.',alpha = 1, markersize =5) 

def init(): 
    points.set_data([], []) 
    return points, 

# animation function. This is called sequentially 
def animate(i): 
    x = test["dropoff_longitude"] 
    y = test["dropoff_latitude"] 
    points.set_data(x, y) 
    return points, 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=100, interval=20, blit=True) 

plt.show() 

enter image description here

+0

感谢您的评论,但该解决方案无法正常工作。我也尝试删除alpha参数,但没有运气。 – madsthaks

+0

它确实有效,看到更新的答案。 – ImportanceOfBeingErnest

+0

那很令人沮丧。它仍然不起作用,我不太清楚如何调试它。 – madsthaks