I am plotting the location of earthquake at various places using lattitude and longitude. I use geojson data to plot a map and then use columns name 'Longitude' and 'Latitude' to plot the marker on map. But the color of marker and color bar is not same the circle with 8.2 marker should be more dark red as per color bar but it is yellowish in map. How to solve this?
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=1
)
# 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()
[]
You have set
alpha=0.5
. Alpha is a measure of "transparency"; in your case, the marker color is 50% mixed with the background blue. Setalpha=1
if you want the markers to have a solid color matching the color scale.Also, you forgot to normalize the color. If you share your dataframe I can make an example ad hoc. In the meantime, look at this toy example:
Try this modified version of your code: