matplotlib 如何配置标记的边缘颜色?

hgqdbh6s  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(91)

我正试着画一张带有点的Map。我想让传奇中的标记有一个黑色的边缘。如何配置标记的边缘颜色?
下面是我的代码:

import geopandas as gpd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt

# 创建一个包含经纬度坐标的GeoDataFrame
df = df_unique
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['modern_longitude'], df['modern_latitude']),crs='EPSG:4326')

#创建投影对象
robinson = ccrs.Robinson()

# 将 GeoDataFrame 投影到 Robinson 投影坐标系下
gdf_robinson = gdf.to_crs(ccrs.Robinson().proj4_init)

# 读取地图数据
map_path = "110m_physical/ne_110m_land.shp"
world = gpd.read_file(map_path)
world=world.to_crs(ccrs.Robinson().proj4_init)

# 创建绘图对象
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1, projection=robinson)
ax2 = ax

# 添加数据
world.plot(ax=ax2, color='lightgray', edgecolor='black')
gdf_robinson.plot(ax=ax, legend=True, legend_kwds = {"loc":'center left', "bbox_to_anchor": (1.1, 0.5),"edgecolor":'black',"title":"proxy"},column='proxy',cmap='tab20', markersize=50,edgecolor='black')

# ax.set_axis_off()
plt.show()

字符串
这是当前的图enter image description here

qnzebej0

qnzebej01#

Q: How can I config marker's edge color?
A: Just follow these steps:-

1. Get the legend object, here 'leg1' is the legend object: 

   leg1 = ax.get_legend()   # Maybe ax2, make your choice

2. Iterate through all its handles, and set its property

    for ea in leg1.legendHandles:
        #ea.set_marker('s')          # Set square shape
        ea.set_markeredgecolor('k')  # Set black edge (you ask for it)

字符串

相关问题