I'm plotting scatter points onto a map and seeing unwanted rectangles in my legend, despite the insertion of label='_nolegend_'
:
# 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()
How to remove the rectangles, and just show the scatter point in the legend?
The problem is that you set labels for each of the elements in the axes via ('','','San Francisco')
. Instead just set the label to the scatter itself
ax.scatter(..., label="Some City")
ax.legend(loc=3)
Alternatively, if you don't want to give the scatter a label, you can pass the handle and label to the legend
:
sc = ax.scatter(...)
ax.legend(handles=[sc], labels=['Some City'], loc = 3)