Home > OS >  How do I assign custom colors to values as points using geopandas?
How do I assign custom colors to values as points using geopandas?

Time:11-09

I made a map made in geopandas. However, I am using geopandas, regular pandas, matplotlib, and shapely.geometry to do different things. I need the colors on my map to be custom, assigned to specific values in a column. More specifically, I have a column of bird names and want each bird to have its own custom color. The map below shows the bird sightings. How do I add custom colors for the bird sightings? Would I use geopandas, pandas or something else?

import pandas as pd
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon

# Create a new dataframe
france_df = pd.read_csv('bird_tracking.csv')

# Filter the dataset for France latitude and longitude range
france_df = france_df.query('-3 < longitude < 4')
france_df = france_df.query('45 < latitude < 51')

# Designate the right coordinate system (maps can be drawn differently)
crs = 'EPSG:4326'

# Takes the lat and long from our bird sightings, puts into list of single points (shapely)
geometry = [Point(xy) for xy in zip(france_df['longitude'], france_df['latitude'])]

# Create GeoPandas dataframe (includes our France bird tracking data frame)
geo_df = gpd.GeoDataFrame(france_df, crs = crs, geometry = geometry)

# Create figure and map axes, assign to subplot (matplotlib)
fig, ax = plt.subplots(figsize=(15, 15))

# Assign colors to each bird name
bird_palette = {'Boba': 'turquoise', 'Kiwi': 'green', 'Pretzel' : 'brown'}

# Specify France map coordinates and add more formatting
map_france = geo_df.to_crs('EPSG:3857').plot(column='bird_name', linewidth=2, ax=ax, alpha=0.5, legend=True, markersize=10)
ctx.add_basemap(map_france, source=ctx.providers.CartoDB.Positron)

enter image description here

Here's the original dataset: enter image description here

CodePudding user response:

You can use the enter image description here

  • Related