我试图为我的文书工作制作一个程序,该程序将实时显示两个城市之间的范围,我想要但我遇到了一个我无法处理的问题。
代码:
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
# Create new figure, axes instances.
fig = plt.figure()
ax = fig.add_axes([0.1, 0.3, 0.8, 0.6]) # Adjusted axes position for the map.
# Setup mercator map projection.
m = Basemap(llcrnrlon=-180., llcrnrlat=-60., urcrnrlon=180., urcrnrlat=85.,
rsphere=(6378137.00, 6356752.3142),
resolution='l', projection='merc',
lat_0=40., lon_0=-20., lat_ts=20.)
# Define latitude and longitude coordinates for Singapore (sglat, sglon) and San Francisco (sflat, sflon).
sglat = 1.35
sglon = 103.82
sflat = 37.77
sflon = -122.42
# Calculate points along the great circle route.
num_points = 1000
lons, lats = m.gcpoints(sglon, sglat, sflon, sflat, num_points)
# Draw great circle route between Singapore and San Francisco.
m.drawgreatcircle(sglon, sglat, sflon, sflat, linewidth=2, color='b')
m.drawcoastlines()
m.fillcontinents()
# Draw parallels and meridians.
m.drawparallels(np.arange(-90, 90, 20), labels=[1, 1, 0, 1])
m.drawmeridians(np.arange(-180, 180, 30), labels=[1, 1, 0, 1])
ax.set_title('Great Circle from Singapore to San Francisco')
# Create a slider for animation.
slider_ax = fig.add_axes([0.25, 0.1, 0.5, 0.03])
slider = Slider(slider_ax, 'Position', 0, 1, valinit=0)
# Initialize the marker.
marker, = ax.plot([], [], marker='o', markersize=8, color='r')
# Function to update marker position based on slider value.
def update_marker(val):
position = int(val * (num_points - 1)) # Calculate the index along the route.
lon, lat = lons[position], lats[position]
marker.set_data([lon], [lat]) # Pass lon and lat as sequences
fig.canvas.draw_idle()
# Connect the slider to the update function.
slider.on_changed(update_marker)
plt.show()
所以一般我希望整个路线显示在中间。非常感谢您的帮助。
解决方案:
将 lon_0“地图中心经度”偏移为您的旅行路径中心左侧 180 度(直线距离)。