python - 在 Cartopy 地图中实时更新国际空间站位置
问题描述
我正在编写简单的脚本,以使用 cartopy 在世界地图上定位 ISS。这是代码及其输出
import requests
from datetime import datetime
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
# finding the current location
url = 'http://api.open-notify.org/iss-now.json'
r = requests.get(url)
data = r.json()
dt = data['timestamp']
lat = data['iss_position']['latitude']
lon = data['iss_position']['longitude']
print('Date and time : ',datetime.fromtimestamp(dt))
print('Latitude :',lat)
print('Longitude :',lon)
# plotting the location on map
plt.figure(figsize=(10,8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
# lon lat
plt.scatter(float(lon),float(lat),color='blue', marker='o')
plt.show()
这部分工作正常,这是输出。
现在我想要的是每 30 秒向 url 发出请求,并在地图上绘制位置以清除先前的位置。是否可以与 matplotlib. 我对matplotlib了解不多。请帮忙。
我当前的代码每次都会创建带有散点的新图。我只想要一张当前位置和先前位置已被删除的地图。
这是我当前的代码
import requests
import matplotlib.pyplot as plt
import time
import cartopy.crs as ccrs
url = 'http://api.open-notify.org/iss-now.json'
plt.figure(figsize=(10,8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
plt.ion()
while True:
r = requests.get(url)
data = r.json()
dt = data['timestamp']
lat = data['iss_position']['latitude']
lon = data['iss_position']['longitude']
plt.scatter(float(lon),float(lat),color='blue', marker='o')
plt.show()
time.sleep(30)
解决方案
matplotlib.animation.FuncAnimation
专门为这种情节而存在。尝试这个
import requests
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
import cartopy.crs as ccrs
url = 'http://api.open-notify.org/iss-now.json'
plt.figure(figsize=(10,8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
plt.ion()
plt.show()
s = plt.scatter([], [], color='blue', marker='o')
def animate(i):
r = requests.get(url)
data = r.json()
dt = data['timestamp']
lat = data['iss_position']['latitude']
lon = data['iss_position']['longitude']
s.set_offsets([float(lon), float(lat)])
time.sleep(30)
anim = FuncAnimation(plt.gcf(), animate)
plt.show()
这将显示(加速演示):
推荐阅读
- javascript - WordPress WPRocket:延迟加载 css 背景图片
- android - 使用 FirebaseRecyclerAdapter 的 Firebase 搜索实现
- powerbi - 如何找到在 Power BI 中使用 max withing 计算的替代方法
- tinymce - TinyMCE 5 手动调整大小
- batch-file - Windows 上的 GitLab 运行器并处理 & %ERRORLEVEL%
- video - 如何以编程方式记录视频更改的时间?
- python - How to extract apartment from address in Pandas
- javascript - 将 Angular IO 与 extern html 文件一起使用(独立)
- c# - 以 pdf 格式输出 С# iTextSharp
- jsf - Will Primefaces not automatically generate ID for html tag JSF?