我正在尝试绘制漂浮在海中的点。以下代码有效,但也绘制了前面绘图的所有点:
Duration = 6 #hours
plt.figure(figsize=(20,10))#20,10
map = Basemap(width=300000,height=300000,projection='lcc',
resolution='c',lat_0=51.25,lon_0=-4)#lat_0lon_0 is left under
map.drawmapboundary(fill_color='turquoise')
map.fillcontinents(color='white',lake_color='aqua')
map.drawcountries(linestyle='--')
x=[]
y=[]
for i in range (0,Duration):
x,y = map(Xpos1[i],Ypos1[i])
map.scatter(x, y, s=1, c='k', marker='o', label = 'Aurelia aurita', zorder=2)
plt.savefig('GIF10P%d' %i)
Xpos1 和 Ypos1 是掩码数组列表。列表中的每个数组的长度为 10,因此每张图中应绘制 10 个点:
Xpos1=[[latitude0,lat1,lat2,lat3,..., lat9],
[latitude0,lat1,lat2,lat3,..., lat9],...]
每张图片应该有 10 分,但最后一张是所有 map 的组合(所以 60 分)。 我如何仍然获得 6 张 map ,每张 map 只有 10 点?
编辑: 当我使用 matplotlib.pyplot will not forget previous plots - how can I flush/refresh? 的答案时我得到错误
ValueError: Can not reset the axes. You are probably trying to re-use an artist in more than one Axes which is not supported
当我使用来自 How to "clean the slate"? 的答案时,会弹出类似的错误 即,
plt.clf()
plt.cla()#after plt.show()
非常感谢任何帮助!
最佳答案
与其为每个图像绘制新的散点图,不如更新散点图的数据。优点是 map 只需要创建一次,节省了一些时间。
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
Duration = 6 #hours
Xpos1 = np.random.normal(-4, 0.6, size=(Duration,10))
Ypos1 = np.random.normal(51.25, 0.6, size=(Duration,10))
plt.figure(figsize=(20,10))
m = Basemap(width=300000,height=300000,projection='lcc',
resolution='c',lat_0=51.25,lon_0=-4)
m.drawmapboundary(fill_color='turquoise')
m.fillcontinents(color='white',lake_color='aqua')
m.drawcountries(linestyle='--')
scatter = m.scatter([], [], s=10, c='k', marker='o', label = 'Aurelia aurita', zorder=2)
for i in range (0,Duration):
x,y = m(Xpos1[i],Ypos1[i])
scatter.set_offsets(np.c_[x,y])
plt.savefig('GIF10P%d' %i)
plt.show()
关于python - 在不绘制先前点的情况下循环绘制 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50098388/