在地图上绘制以公里为单位、以纬度和经度为中心点的网格

问题描述 投票:0回答:1

我想绘制一个大小为 2km x 2km 的方形网格,其中点(纬度,经度)以度为单位:

co_ord = (47.9187393, 106.9175013)

位于方格的中心。

我已经尝试过:

import pandas as pd
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# setting the size of the map
fig = plt.figure(figsize=(12,9))

# create the map - setting latitude and longitude
m = Basemap( projection = 'mill', llcrnrlat = 47.85, urcrnrlat = 48.1, llcrnrlon = 106.2, urcrnrlon = 107.1, resolution ='h')

m.drawcoastlines()
m.drawcountries(color='gray')
m.drawstates(color='gray')

# creating variable for latitude, longitude to list
lat = 47.9187393 
lon = 106.9175013

# plotting the map
m.scatter(lon, lat, latlon = True, s = 10, c = 'red', marker = 'o', alpha = 1)

plt.show()

但是,我还想显示地理位置以及绘图的 x 轴和 y 轴(以米为单位),坐标位于绘图的 (0,0) 处。

python matplotlib google-maps latitude-longitude matplotlib-basemap
1个回答
0
投票

使用

xticks
yticks
绘制刻度,然后添加标签。

代码:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

# setting the size of the map
fig = plt.figure(figsize=(12,9))

# create the map - setting latitude and longitude
m = Basemap(projection='mill', llcrnrlat=47.85, urcrnrlat=48.1, llcrnrlon=106.2, urcrnrlon=107.1, resolution='h')

m.drawcoastlines()
m.drawcountries(color='gray')
m.drawstates(color='gray')

# Define the center point
center_lat, center_lon = 47.9187393, 106.9175013

# Convert center point to meters
center_x, center_y = m(center_lon, center_lat)

# Define the size of the square grid in meters
grid_size = 2000  # 2km

# Define the boundaries of the square grid
x_min, x_max = center_x - grid_size / 2, center_x + grid_size / 2
y_min, y_max = center_y - grid_size / 2, center_y + grid_size / 2

# Plot the square grid
m.plot([x_min, x_min, x_max, x_max, x_min], [y_min, y_max, y_max, y_min, y_min], color='blue')

# Plot the center point
m.scatter(center_lon, center_lat, latlon=True, s=50, c='red', marker='o', alpha=1)

# Convert meters to geographical coordinates for ticks
def meters_to_lonlat(x, y):
    lon, lat = m(x, y, inverse=True)
    return lon, lat

# Define x and y ticks
x_ticks = np.arange(x_min, x_max, 500)
y_ticks = np.arange(y_min, y_max, 500)

# Convert ticks to lon/lat
lon_ticks, lat_ticks = meters_to_lonlat(x_ticks, y_ticks)

# Plot grid ticks
m.scatter(lon_ticks, lat_ticks, latlon=True, s=10, c='black', marker='.')

# Set x and y axis labels to meters
plt.xlabel('X (meters)')
plt.ylabel('Y (meters)')

# Set the tick labels
plt.xticks(x_ticks, [f'{int(t)}\n({int(t - center_x)}m)' for t in x_ticks])
plt.yticks(y_ticks, [f'{int(t)}\n({int(t - center_y)}m)' for t in y_ticks])

plt.show()

pyplot xticks

© www.soinside.com 2019 - 2024. All rights reserved.