我正试图根据季节性来评估一种特定鱼类在海底的位移。因此,我想根据检测发生的月份创建一个带有不同颜色点的地图(例如,8月份的所有点为蓝色,9月份的所有点为红色,10月份的所有点为黄色)。
在我的数据框架中,我有每个点的坐标(Lat, Lon)和检测日期(dates):
<表类>
LAT
经度
日期
tbody><<tr>0 49.302005 -67.684971 2019-08-06 149.302031 -67.684960 2019-08-12 249.302039 -67.684983 2019-08-21 3 49.302039 -67.684979 2019-08-30 449.302041 -67.684980 2019-09-03 549.302041 -67.684983 2019-09-10 649.302042 -67.684979 2019-09-18 749.302043 -67.684980 2019-09-25 849.302045 -67.684980 2019-10-01 9 49.302045 -67.684983 2019-10-09 1049.302048 -67.684979 2019-10-14 1149.302049 -67.684981 2019-10-21 12 49.302049 -67.684982 2019-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()