我正在尝试绘制在海中漂流的点.以下代码可以正常工作,但也可以绘制先前图形的所有点:
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点,但是最后一张是所有地图的组合(所以有60点).
我如何仍然获得6张仅含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()
任何帮助深表感谢!
解决方法:
与其为每个图像绘制新的散点图,不如更新散点图的数据.好处是该地图仅需要创建一次,从而节省了一些时间.
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()