Matplotlib动画显示为空

2024-04-24 06:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个类似的问题here,但我没有同样的问题。以下是我的数据集快照:

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()

与上述问题中的问题类似,我的情节只是空洞地出现。我相信我的编码是正确的,与上面的链接不同,我确实看到坐标随着时间的推移而变化。我不知道为什么这个情节是空的。在


Tags: datareturninitdef时间figpltax
1条回答
网友
1楼 · 发布于 2024-04-24 06:26:37

默认情况下,一个像素为1点或0.72点(取决于您是在jupyter笔记本中运行代码还是作为独立绘图运行代码)。如果创建markersize为0.05的绘图,则每个标记的大小分别为0.05像素或{}像素。由于在屏幕上很难看到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

相关问题 更多 >