python - 从 Matplotlib 图例中排除 Cartopy 元素

标签 python matplotlib cartopy

尽管插入了 label='_nolegend_',但我正在将散点绘制到 map 上,并在图例中看到不需要的矩形:

# import functions
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

# Create a Stamen terrain background instance
stamen_terrain = cimgt.Stamen('terrain-background')
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1, 1, 1, projection=stamen_terrain.crs, label='_nolegend_')

# Set range of map, stipulate zoom level
ax.set_extent([-122.7, -121.5, 37.15, 38.15], crs=ccrs.Geodetic())
ax.add_image(stamen_terrain, 12, label='_nolegend_')

# Add scatter point
ax.scatter(-122.4194, 37.7749, s=55, c='k', transform=ccrs.PlateCarree())    
ax.legend(('','','San Francisco'), loc = 3)
plt.show()

如何删除矩形,只在图例中显示散点?

Legend has unwanted rectangles

最佳答案

问题是您通过 ('','','San Francisco') 为轴中的每个元素设置标签。相反,只需将标签设置为分散本身

ax.scatter(..., label="Some City")
ax.legend(loc=3)

或者,如果您不想为散点图提供标签,则可以将句柄和标签传递给图例:

sc = ax.scatter(...)    
ax.legend(handles=[sc], labels=['Some City'], loc = 3)

关于python - 从 Matplotlib 图例中排除 Cartopy 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56810837/

相关文章:

python - 百分比堆积条形图 Pandas

python - Airflow GKEPodOperator xcom_push 返回 None

python - PyQt 启动时加载函数(qml 加载器)

python - 在 center_longitude 上的 cartopy 等高线图中的白线

python - 在 cartopy map 中绘制网格线

python - 如何动态更改 __slots__ 属性?

python - 如何禁用 matplotlib 中的日期插值?

python - 带有颜色图的散点图使 X 轴消失

python - 导入错误 : No module named matplotlib in IDLE

python - 旋转纬度 (y) 刻度标签 - cartopy