标记颜色和颜色条在地理图中不匹配

The marker color and colorbar are not matching in geoplot

提问人:Purnima Rawal 提问时间:11/9/2023 最后编辑:Trenton McKinneyPurnima Rawal 更新时间:11/12/2023 访问量:26

问:

我正在使用纬度和经度绘制各个地方的地震位置。我使用 geojson 数据绘制地图,然后使用名为“经度”和“纬度”的列在地图上绘制标记。但是标记和彩条的颜色不匹配。如何解决这个问题?

Here is the image

geojson_path = '/content/drive/MyDrive/ColabNotebooks/Practice/nepal.geojson'
gdf = gpd.read_file(geojson_path)


# Read CSV file using Pandas
mydata = pd.read_csv('/content/drive/MyDrive/ColabNotebooks/Practice/earthquakes.tsv', sep='\t')
df = mydata[mydata['Mag'].notnull()]

# Create a GeoDataFrame from the DataFrame with latitude and longitude
geometry = gpd.points_from_xy(df['Longitude'], df['Latitude'])
gdf_points = gpd.GeoDataFrame(df, geometry=geometry)
# Set up the subplots to take up the whole width
fig, ax = plt.subplots(figsize=(15, 8)) 

# Define a colormap and normalize values based on the 'Mag' column
cmap = plt.get_cmap('YlOrRd')  # You can choose any other colormap
norm = Normalize(vmin=df['Mag'].min(), vmax=df['Mag'].max())


# Plot the GeoDataFrame with points, adjusting circle size based on the 'size' column
gdf.plot(ax=ax)

scatter = gdf_points.plot(
    ax=ax, 
    cmap=cmap,
    markersize=(df['Mag']) * 200,
    c=df['Mag'],  # Assign 'Mag' values as the color
    norm=norm,
    alpha=0.5
)

# Add colorbar for reference
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])  # You need to set an array to the ScalarMappable
cbar = plt.colorbar(sm, ax=ax, label='Magnitude')

# Annotate each point with its 'size' value
for x, y, label in zip(df['Longitude'], df['Latitude'], df['Mag']):
    plt.text(x, y, str(label), color='white', ha='center', va='center')

plt.title('Map with Points from CSV')
plt.show()
蟒蛇 python-3.x matplotlib geojson 颜色条

评论


答: 暂无答案