我正在用Python和Cartopy / matplotlib编写一个程序,在世界地图上绘制数据。在我看来,某些地方缺少Cartopy文档,但是幸运的是,到目前为止,我已经从该站点获得了很大的帮助。
地图必须能够根据用户的数据进行缩放。因此,我需要以编程方式确定图块的缩放级别和地图的范围。至少对我来说,我想出了一种方法来使似乎正确,但是结果并不完全符合我的期望。这是我当前的代码:
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
from matplotlib.pyplot import figure
def get_zoom(area):
A = [180*360]
for i in range(1,20):
A.append(A[i-1]/(4*i))
for level in A:
if area >= level:
zoom = A.index(level)
break
#Bumping the zoom up by 1 improved the results bit
if zoom < 20:
zoom += 1
return zoom
def custom_background(source_point):
source_point = source_point.split(" ")
source_point = (float(source_point[0]), float(source_point[1]))
dx = 2
dy = 2
lon_min, lon_max = source_point[0]-dx, source_point[0]+dx
lat_min, lat_max = source_point[1]-dy, source_point[1]+dy
area = 4*dx*dy
zoom = get_zoom(area)
tile = cimgt.OSM()
ax = plt.axes(projection=ccrs.PlateCarree())
pad = 2
ax.set_extent([lat_min-pad, lat_max+pad, lon_min-pad, lon_max+pad])
ax.add_image(tile, zoom)
return ax
custom_background("45.068466 -66.45477")
## One thing I tried was increasing the dpi with this method:
#figure(num=1, figsize=(8,6), dpi=800)
##I also tried setting it in the savefig method, neither made a noticeable difference
plt.savefig("tile.png")
我尝试过的事情:
到目前为止的结果:
无论我如何调整,线条和名称总是有点模糊。有人可以帮忙吗?