在 Matplotlib 中实时绘制图像

发布于 2025-01-12 13:42:47 字数 2794 浏览 4 评论 0原文

我正在尝试在地图上绘制卫星地面轨迹,目前我正在使用 cartopy,matplotlib

卫星的通过是单独计算的,并保存在 .CSV 文件中,当我尝试绘制路径时,结果很棒且准确,但它是完成一次(完整路径被绘制为一个整体)

在此处输入图像描述

但我需要逐点绘制它并且每次更新该数字添加了新点。

这是我在这个问题中使用的代码:

# imports
import cartopy.crs as ccrs
import pandas as pd
import matplotlib.pyplot as plt
from cartopy.feature.nightshade import Nightshade

import datetime

# reading CSV file
data = pd.read_csv("longlat.csv")

# converting column data to list of latitudes and longitudes
latitudes = data['Latitude'].tolist()
longitudes = data['Longitude'].tolist()

# Create a new figure, or activate an existing figure.
fig = plt.figure(figsize=(15.2, 8.2))
# Add an Axes to the current figure
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

# time to set the Nightshade to
# date = datetime.datetime(2022, 3, 8, 20, 9, 00)
date = datetime.datetime.now()

ax.stock_img()
# add Nightshade tp the map according to the time
ax.add_feature(Nightshade(date, alpha=0.3))

# set the x and y limits of the current axes.
plt.xlim([-180, 180])
plt.ylim([-90, 90])

# Configure the grid lines displayed on the map with the labels om x amd y.
plt.grid(True, color='w', linestyle="dotted", alpha=0.3, linewidth=1)
plt.xticks(range(-180, 181, 30), ['-180°W', '-150°W', '-120°W', '-90°W', '-60°W', '-30°W', '0°', '30°E', '60°E',
                                  '90°E', '120°E', '150°E', '180°E'])
plt.yticks(range(-90, 91, 30), ['-90°S', '-60°S', '-30°S', '0°', '30°N', '60°N', '90°N'])

# Adjust the padding between and around subplots.
plt.tight_layout()


def plot_city(cities_list):
    """
    function plot cities location points on the map
    :param cities_list: list like object
    list of cities to plo on the map ['city name', longitude, latitude]
    :return: None
    """
    for city in cities_list:
        # plot city on the map
        plt.plot(city[1], city[2], marker="o", markersize=5, markeredgecolor="blue", markerfacecolor="green")
        # plot city name on the map according to it's location
        plt.text(city[1] - 1, city[2] - 1, city[0], horizontalalignment='right', fontsize=8, transform=ccrs.Geodetic())


cities = [['Cairo', 29.35, 30.03333], ['Moscow', 37.618423, 55.751244], ['Paris', 2.349014, 48.864716]]

# sample city location plotting  (cairo)
plot_city(cities)


def satellite_plot(longitudes, latitudes):
    # plot the satellite moving line
    plt.plot(longitudes, latitudes, 'red', linestyle="dotted", linewidth=2, transform=ccrs.Geodetic())
    satellite = plt.plot(longitudes, latitudes, marker="o", markersize=5,
                         markeredgecolor="blue", markerfacecolor="green")
    line = satellite.pop(0)
    line.remove()


satellite_plot(longitudes, latitudes)
plt.show()

I'm trying to plot the satellite ground track on a map currently I'm using cartopy, matplotlib

the pass of the satellite is calculated separately and saved in.CSV file when I try to plot the path the result is great and accurate but it's done once (the full path is plotted as a whole)

enter image description here

but I need to plot it point by point and update the figure each time a new point is added.

this is the code I used in this problem:

# imports
import cartopy.crs as ccrs
import pandas as pd
import matplotlib.pyplot as plt
from cartopy.feature.nightshade import Nightshade

import datetime

# reading CSV file
data = pd.read_csv("longlat.csv")

# converting column data to list of latitudes and longitudes
latitudes = data['Latitude'].tolist()
longitudes = data['Longitude'].tolist()

# Create a new figure, or activate an existing figure.
fig = plt.figure(figsize=(15.2, 8.2))
# Add an Axes to the current figure
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

# time to set the Nightshade to
# date = datetime.datetime(2022, 3, 8, 20, 9, 00)
date = datetime.datetime.now()

ax.stock_img()
# add Nightshade tp the map according to the time
ax.add_feature(Nightshade(date, alpha=0.3))

# set the x and y limits of the current axes.
plt.xlim([-180, 180])
plt.ylim([-90, 90])

# Configure the grid lines displayed on the map with the labels om x amd y.
plt.grid(True, color='w', linestyle="dotted", alpha=0.3, linewidth=1)
plt.xticks(range(-180, 181, 30), ['-180°W', '-150°W', '-120°W', '-90°W', '-60°W', '-30°W', '0°', '30°E', '60°E',
                                  '90°E', '120°E', '150°E', '180°E'])
plt.yticks(range(-90, 91, 30), ['-90°S', '-60°S', '-30°S', '0°', '30°N', '60°N', '90°N'])

# Adjust the padding between and around subplots.
plt.tight_layout()


def plot_city(cities_list):
    """
    function plot cities location points on the map
    :param cities_list: list like object
    list of cities to plo on the map ['city name', longitude, latitude]
    :return: None
    """
    for city in cities_list:
        # plot city on the map
        plt.plot(city[1], city[2], marker="o", markersize=5, markeredgecolor="blue", markerfacecolor="green")
        # plot city name on the map according to it's location
        plt.text(city[1] - 1, city[2] - 1, city[0], horizontalalignment='right', fontsize=8, transform=ccrs.Geodetic())


cities = [['Cairo', 29.35, 30.03333], ['Moscow', 37.618423, 55.751244], ['Paris', 2.349014, 48.864716]]

# sample city location plotting  (cairo)
plot_city(cities)


def satellite_plot(longitudes, latitudes):
    # plot the satellite moving line
    plt.plot(longitudes, latitudes, 'red', linestyle="dotted", linewidth=2, transform=ccrs.Geodetic())
    satellite = plt.plot(longitudes, latitudes, marker="o", markersize=5,
                         markeredgecolor="blue", markerfacecolor="green")
    line = satellite.pop(0)
    line.remove()


satellite_plot(longitudes, latitudes)
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文