如何为添加 Cartopy 形状特征的 GeoAxes 添加图例?
How to add a legend for a GeoAxes that adds a Cartopy shapely feature?
我复制了 adding legend via proxy artists from matplotlib's documentation but it doesn't work. I also tried the rest in matplotlib's legends guide 的代码,但没有任何效果。我想这是因为该元素是一个 ax.legend()
无法识别的形状特征。
代码
bounds = [116.9283371, 126.90534668, 4.58693981, 21.07014084]
stamen_terrain = cimgt.Stamen('terrain-background')
fault_line = ShapelyFeature(Reader('faultLines.shp').geometries(), ccrs.epsg(32651),
linewidth=1, edgecolor='black', facecolor='none') # geometry is multilinestring
fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent(bounds)
ax.add_image(stamen_terrain, 8)
a = ax.add_feature(fault_line, zorder=1, label='test')
ax.legend([a], loc='lower left', fancybox=True) #plt.legend() has the same result
plt.show()
结果
复制 matplotlib 示例时,您省略了实际的“代理”艺术家行!
red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])
那个red_patch
是代理艺人。您必须创建一个虚拟艺术家才能传递给 legend()
。您编写的代码仍然通过无法识别的 Shapely 功能。
这很乏味,但相关代码应该是这样的:
fault_line = ShapelyFeature(Reader('faultLines.shp').geometries(), ccrs.epsg(32651), linewidth=1, edgecolor='black', facecolor='none')
ax.add_feature(fault_line, zorder=1)
# Now make a dummy object that looks as similar as possible
import matplotlib.patches as mpatches
proxy_artist = mpatches.Rectangle((0, 0), 1, 0.1, linewidth=1, edgecolor='black', facecolor='none')
# And manually add the labels here
ax.legend([proxy_artist], ['test'], loc='lower left', fancybox=True)
这里我只使用了一个Rectangle
,但是根据功能,你可以使用各种支持的matplotlib“艺术家”。
我复制了 adding legend via proxy artists from matplotlib's documentation but it doesn't work. I also tried the rest in matplotlib's legends guide 的代码,但没有任何效果。我想这是因为该元素是一个 ax.legend()
无法识别的形状特征。
代码
bounds = [116.9283371, 126.90534668, 4.58693981, 21.07014084]
stamen_terrain = cimgt.Stamen('terrain-background')
fault_line = ShapelyFeature(Reader('faultLines.shp').geometries(), ccrs.epsg(32651),
linewidth=1, edgecolor='black', facecolor='none') # geometry is multilinestring
fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent(bounds)
ax.add_image(stamen_terrain, 8)
a = ax.add_feature(fault_line, zorder=1, label='test')
ax.legend([a], loc='lower left', fancybox=True) #plt.legend() has the same result
plt.show()
结果
复制 matplotlib 示例时,您省略了实际的“代理”艺术家行!
red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])
那个red_patch
是代理艺人。您必须创建一个虚拟艺术家才能传递给 legend()
。您编写的代码仍然通过无法识别的 Shapely 功能。
这很乏味,但相关代码应该是这样的:
fault_line = ShapelyFeature(Reader('faultLines.shp').geometries(), ccrs.epsg(32651), linewidth=1, edgecolor='black', facecolor='none')
ax.add_feature(fault_line, zorder=1)
# Now make a dummy object that looks as similar as possible
import matplotlib.patches as mpatches
proxy_artist = mpatches.Rectangle((0, 0), 1, 0.1, linewidth=1, edgecolor='black', facecolor='none')
# And manually add the labels here
ax.legend([proxy_artist], ['test'], loc='lower left', fancybox=True)
这里我只使用了一个Rectangle
,但是根据功能,你可以使用各种支持的matplotlib“艺术家”。