在Python中对图像进行动画处理

时间:2020-05-21 20:06:20

标签: python pandas matplotlib

我正在尝试对图像上的地理坐标图进行动画处理,在本例中为地图的摘要。我设法产生了一个静态图,但是无法对其进行动画处理。我尝试使用matplotlib动画功能制作动画,但是还没有成功。我正在使用熊猫将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

我仍然是新手;任何帮助表示赞赏。

2 个答案:

答案 0 :(得分: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,

答案 1 :(得分:0)

如果要直接在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()