如何在Python中通过将点组与检测日期关联来绘制地图上的点组



我正试图根据季节性来评估一种特定鱼类在海底的位移。因此,我想根据检测发生的月份创建一个带有不同颜色点的地图(例如,8月份的所有点为蓝色,9月份的所有点为红色,10月份的所有点为黄色)。

在我的数据框架中,我有每个点的坐标(Lat, Lon)和检测日期(dates):

<表类> LAT 经度 日期 tbody><<tr>049.302005-67.6849712019-08-06149.302031-67.6849602019-08-12249.302039-67.6849832019-08-21349.302039-67.6849792019-08-30449.302041-67.6849802019-09-03549.302041-67.6849832019-09-10649.302042-67.6849792019-09-18749.302043-67.6849802019-09-25849.302045-67.6849802019-10-01949.302045-67.6849832019-10-091049.302048-67.6849792019-10-141149.302049-67.6849812019-10-211249.302049-67.6849822019-10-29

这里有一种完全使用Pandas和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

如果您希望将月份作为名称而不是索引,并使用seaborn进行更花哨的绘图(例如,使用图例标记日期),您可以这样做:

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()

最新更新