【问题标题】:Plot map in loop without plotting previous points在循环中绘制地图而不绘制先前的点
【发布时间】:2018-04-30 10:14:43
【问题描述】:

我正在尝试绘制在海中漂流的点。以下代码有效,但也绘制了先前绘图的所有点:

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()

非常感谢任何帮助!

【问题讨论】:

  • 是只有最后一张图是所有图的组合,还是除了第一张以外的所有图都是之前所有图的组合?
  • 另外,请说明map 对象(在其上调用scatter 方法)的来源。
  • scatter 似乎在现有情节中添加了新点。您可能需要在每次保存后清除或重新创建它。
  • 尝试将 for 循环外的内容移到顶部。
  • 作为一个小点 map 是一个关键字 - 所以避免它作为变量 - 在你为问题添加额外细节之前让我感到困惑

标签: python loops matplotlib


【解决方案1】:

与其为每个图像绘制新的散点图,不如更新散点图的数据。优点是地图只需要创建一次,节省一些时间。

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()

【讨论】:

    【解决方案2】:

    正如@Primusa 所说,只需将所有内容移入 for 循环即可重新定义地图。 那么正确的代码是:

    for i in range (0,Duration,int(24/FramesPerDay)):
        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 = 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)
    

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 2021-11-22
      • 2020-11-08
      • 2018-10-02
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多