【问题标题】:Animate a plot over an image in Python在 Python 中为图像绘制动画
【发布时间】:2020-05-21 20:06:20
【问题描述】:

我正在尝试为图像上的地理坐标图制作动画,在本例中为地图的 sn-p。我设法制作了一个静态图,但无法让它动画化。我尝试使用 matplotlib 动画功能制作动画,但没有取得任何成功。我正在使用 pandas 将 csv 读入 Python 并使用 matplotlib.pyplot 进行绘图。下面是静态图的代码。

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('mydata.csv', header=0)
# find max/min, plug into a website, snip area as png and insert as plotmap
BBox = ((df.LONGITUDE.min(),   df.LONGITUDE.max(),
         df.LATITUDE.min(), df.LATITUDE.max()))
#read the image in, plot points over image
plotmap = "myimage.png"
truthplot = plt.imread(plotmap)
fig, ax = plt.subplots(figsize = (8,8),linewidth = 0.1)
ax.scatter(df.LONGITUDE, df.LATITUDE, zorder=1, alpha= 0.5, c='b', s=10)
plottitle = "test"
ax.set_title(plottitle)
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
ax.set_xlim(BBox[0], BBox[1])
ax.set_ylim(BBox[2], BBox[3])

ax.imshow(truthplot, zorder=0, extent = BBox, aspect= 'equal')
plt.show()

一些示例坐标:

LATITUDE   LONGITUDE
30.112342  10.678982
29.443459  11.678997
29.334221  11.889544
28.993448  12.003847

我还是个新手;任何帮助表示赞赏。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    
    
    df = pd.read_csv('mydata.csv', header=0)
    # find max/min, plug into a website, snip area as png and insert as plotmap
    BBox = ((df.LONGITUDE.min(),   df.LONGITUDE.max(),
             df.LATITUDE.min(), df.LATITUDE.max()))
    #read the image in, plot points over image
    plotmap = "myimage.png"
    truthplot = plt.imread(plotmap)
    fig, ax = plt.subplots(figsize = (8,8),linewidth = 0.1)
    plottitle = "test"
    ax.set_title(plottitle)
    ax.set_xlabel("Longitude")
    ax.set_ylabel("Latitude")
    ax.set_xlim(BBox[0], BBox[1])
    ax.set_ylim(BBox[2], BBox[3])
    
    scat = ax.scatter(df.LONGITUDE, df.LATITUDE, zorder=1, alpha= 0.5, c='b', s=10)
    color_data = np.random.random((500, len(df.LATITUDE)))
    
    def update(frame):
        scat.set_array(color_data[frame])
        return scat,
    
    ani = FuncAnimation(fig, update, frames=range(500), blit=True)
    
    ax.imshow(truthplot, zorder=0, extent = BBox, aspect= 'equal')
    plt.show()
    

    我不确定你想要动画什么,这就是为什么我只是让这些点闪烁。 但是您可以在更新功能中轻松更改散点图。 scat 是一个 PathCollection,你可以在这里找到它的功能: https://matplotlib.org/3.2.1/api/collections_api.html#matplotlib.collections.PathCollection

    更新

    如果要逐步构建路径,操作 PathCollention 不是很方便。我建议重新创建 obj。

    scat = ax.scatter(df.LONGITUDE[0], df.LATITUDE[0], zorder=1, alpha= 0.5, c='b', s=10)
    
    max_frames = 10
    
    def update(frame):
        scat = ax.scatter(df.LONGITUDE[:(frame * (len(df.LONGITUDE) + 1))//max_frames],
                          df.LATITUDE[:(frame * (len(df.LATITUDE) + 1))//max_frames],
                           zorder=1, alpha= 0.5, c='b', s=10)
        return scat,
    

    【讨论】:

    • 非常接近。本质上,我希望动画从第一行坐标开始并逐行绘制轨迹。我会看看你提供的链接。感谢您的帮助!
    【解决方案2】:

    如果你想直接在 DataFrame 上使用散点图,你可以试试这个:

    BBox = ((df['LONGITUDE'].min(), df['LONGITUDE'].max(),
             df['LATITUDE'].min(), df['LATITUDE'].max()))
    plotmap = 'myimage.png'
    truthplot = plt.imread(plotmap)
    ax = df.plot.scatter(x='LONGITUDE', y='LATITUDE', c='Red')
    plottitle = "test"
    ax.set_title(plottitle)
    ax.set_xlabel("Longitude")
    ax.set_ylabel("Latitude")
    ax.set_xlim(BBox[0], BBox[1])
    ax.set_ylim(BBox[2], BBox[3])
    
    ax.imshow(truthplot, zorder=0, extent = BBox, aspect= 'equal')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 2013-07-20
      • 2018-03-05
      • 1970-01-01
      • 2013-04-25
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多