Python - 动画底图散点图

Python - Animate basemap scatterplot

我有一个形状为 (14403, 438) 的 DataFrame,它由经度和纬度以及值组成。 DataFrame 为:

我将坐标绘制为:

# define map colors
land_color = '#f5f5f3'
water_color = '#cdd2d4'
coastline_color = '#f5f5f3'
border_color = '#bbbbbb'
meridian_color = '#f5f5f3'
marker_fill_color = '#0000ff'
marker_edge_color = 'None'

# create the plot
fig = plt.figure(figsize = (15, 10))
ax = fig.add_subplot(111, facecolor = '#ffffff', frame_on = False)
ax.set_title('Transportable Array', fontsize = 24, color = '#333333')

#lon_0  center of desired map domain (in degrees).
#lat_0  center of desired map domain (in degrees).
#width  width of desired map domain in projection coordinates (meters).
#height height of desired map domain in projection coordinates (meters).

# draw the basemap and its features
m = Basemap(width = 5500000,height = 3300000,
            resolution = 'l', area_thresh = 1000., projection = 'lcc',\
            lat_1 = 45., lat_2 = 55, lat_0 = 37, lon_0 = -98.)
m.drawmapboundary(color = border_color, fill_color = water_color)
m.drawcoastlines(color = coastline_color)
m.drawcountries(color = border_color)
m.fillcontinents(color = land_color, lake_color = water_color)
m.drawparallels(np.arange(-90., 120., 30.), color = meridian_color)
m.drawmeridians(np.arange(0., 420., 60.), color = meridian_color)

# project the location history points then scatter plot them
x, y = m(stations.loc['longitude'].values, stations.loc['latitude'].values)
m.scatter(x, y, s = 8, color = marker_fill_color, edgecolor = marker_edge_color, alpha = 1, zorder = 3)

# show & save the map
plt.savefig('Transportable_Array.png', dpi = 96, bbox_inches = 'tight', pad_inches = 0.2)
plt.show()

我正在尝试创建一个动画来绘制每一列的坐标,然后遍历索引中的值。最后,我试图让它遍历 14,403 行并根据值更改标记颜色。我目前甚至无法单独为坐标绘制动画。

我很想能够实现 bqplot,但是我在 GitHub 上关注的散点动画还没有工作。

地图目前如下所示。如果每个点的颜色都可以根据当前迭代值波动,那就太酷了。

感谢您的阅读。

您可以为此使用动画 模块。这些是一般步骤:

  1. 将值转换为颜色

  2. 每一步更新颜色

  3. 保存动画

这是一些代码:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import matplotlib.animation as animation
import numpy as np

land_color = '#f5f5f3'
water_color = '#cdd2d4'
coastline_color = '#f5f5f3'
border_color = '#bbbbbb'
meridian_color = '#f5f5f3'
marker_fill_color = '#0000ff'
marker_edge_color = 'None'

# Some dummy data
longVals = np.random.uniform(-120,-80, 1000)
latVals = np.random.uniform(35, 45, 1000)
vals = np.random.uniform(size=(200,1000))

# Be careful - the values that go into the colormap function
#  must be integers between 0 and 254
normalisedVals = 254*(vals-vals.min())/(vals.max()-vals.min())
normalisedVals = normalisedVals.astype(np.int)
cm = plt.cm.spectral_r


fig = plt.figure(figsize = (15, 10))
ax = fig.add_subplot(111, facecolor = '#ffffff', frame_on = False)
ax.set_title('Transportable Array', fontsize = 24, color = '#333333')

# draw the basemap and its features
m = Basemap(width = 5500000,height = 3300000,
            resolution = 'l', area_thresh = 1000., projection = 'lcc',
            lat_1 = 45., lat_2 = 55, lat_0 = 37, lon_0 = -98.)
m.drawmapboundary(color = border_color, fill_color = water_color)
m.drawcoastlines(color = coastline_color)
m.drawcountries(color = border_color)
m.fillcontinents(color = land_color, lake_color = water_color)
m.drawparallels(np.arange(-90., 120., 30.), color = meridian_color)
m.drawmeridians(np.arange(0., 420., 60.), color = meridian_color)

x, y = m(longVals, latVals)
scat = m.scatter(x, y, s = 8, c = normalisedVals[0], edgecolor = marker_edge_color, alpha = 1, zorder = 3)


def init():
    return scat,

def animate(i):
    col = cm(normalisedVals[i])
    scat.set_color(col)
    return scat,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=20, blit=False, repeat=False)
anim.save('animation.gif', writer='imagemagick', fps=60)

我应该警告您,对于 14k 行,这将需要一段时间。 此外,由于更好的压缩,我建议保存为 mp4 而不是 gif。 如果您有任何问题,请告诉我!