【问题标题】:cartopy country map with html area links带有 html 区域链接的 cartopy 国家地图
【发布时间】:2013-08-22 12:25:18
【问题描述】:
【问题讨论】:
标签:
python
html
matplotlib
cartopy
【解决方案1】:
是的,这绝对是可能的,但是如果您正在制作基于 Web 的地图,那么可能值得您查看 D3.js(具体而言,有关地图,请参阅此出色的教程 http://bost.ocks.org/mike/map/)。
但是对于 cartopy,我将逐步完成,因为它是 matplotlib 和 cartopy 中转换系统的一个很好的演练。
首先,我们可以得到图中任意一点的像素坐标:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()
# Define a transformation which takes latitude and longitude values,
# and returns pixel coordinates.
ll_to_pixel = ccrs.Geodetic()._as_mpl_transform(ax)
# We need to call draw to ensure that the axes location has been defined
# fully.
plt.draw()
# Now lets figure out the pixel coordinate of Sydney.
x_pix, y_pix = ll_to_pixel.transform_point([151.2111, -33.8600])
# We can even plot these pixel coordinates directly with matplotlib.
plt.plot(x_pix, y_pix, 'ob', markersize=25, transform=None)
plt.savefig('figure_1.png', dpi=plt.gcf().get_dpi())
plt.show()
现在我们有了编写生成区域图的代码所需的信息,我已经继续使用完整的 cmets(https://gist.github.com/pelson/6308997) 发布,以便您可以查看并尝试一下,如果您愿意的话。对于结果的现场演示:https://rawgithub.com/pelson/6308997/raw/map.html