Home > Enterprise >  How to plot groups of points on a map by associating them with the date of detection in Python
How to plot groups of points on a map by associating them with the date of detection in Python

Time:01-19

i'm trying to assess the displacement of a particular fish on the seabed according to seasonality. Thus, i would like to create a map with different colored points according to the month in which the detection occured (e.g., all points from August in blue, all points from Sept in red, all points from Oct in yellow).

In my dataframe i have both coordinates for each point (Lat, Lon) and the dates (Dates) of detection:

LAT LON Dates
0 49.302005 -67.684971 2019-08-06
1 49.302031 -67.684960 2019-08-12
2 49.302039 -67.684983 2019-08-21
3 49.302039 -67.684979 2019-08-30
4 49.302041 -67.684980 2019-09-03
5 49.302041 -67.684983 2019-09-10
6 49.302042 -67.684979 2019-09-18
7 49.302043 -67.684980 2019-09-25
8 49.302045 -67.684980 2019-10-01
9 49.302045 -67.684983 2019-10-09
10 49.302048 -67.684979 2019-10-14
11 49.302049 -67.684981 2019-10-21
12 49.302049 -67.684982 2019-10-29

Would anyone know how to create this kind of map? I know to create a simple map with all points, but i really wonder how plot points associated to the date of detection.

Thank you very much

CodePudding user response:

Here's one way to do it entirely with Pandas and matplotlib:

import pandas as pd
from matplotlib import pyplot as plt

# I'll just create some fake data for the exmaple
df = pd.DataFrame(
    {
        "LAT": [49.2, 49.2, 49.3, 45.6, 467.8],
        "LON": [-67.7, -68.1, -65.2, -67.8, -67.4],
        "Dates": ["2019-08-06", "2019-08-03", "2019-07-17", "2019-06-12", "2019-05-29"]})
    }
)

# add a column containing the months
df["Month"] = pd.DatetimeIndex(df["Dates"]).month

# make a scatter plot with the colour based on the month
fig, ax = plt.subplots()

ax = df.plot.scatter(x="LAT", y="LON", c="Month", ax=ax, colormap="viridis")
fig.show

If you want the months as names rather than indexes, and a slightly more fancy plot (e.g., with a legend labelling the dates) using seaborn, you could do:

import seaborn as sns

# get month as name
df["Month"] = pd.to_datetime(df["Dates"]).dt.strftime("%b")

fig, ax = plt.subplots()
sns.scatterplot(df, x="LAT", y="LON", hue="Month", ax=ax)
fig.show()
  • Related