Skip to content

Commit

Permalink
add year vs region
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Mar 19, 2024
1 parent ceb9708 commit 2a6dede
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions notebooks/wp5/glacier_distribution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@
" title=\"Glacier distribution around the year 2000 according to the RGI v6.0\",\n",
")"
]
},
{
"cell_type": "markdown",
"id": "17",
"metadata": {},
"source": [
"# Plot year VS region"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"metadata": {},
"outputs": [],
"source": [
"def weighted_average(df, field_name, weights_name):\n",
" df = df[df[field_name].notnull() & df[weights_name].notnull()]\n",
" weights = df[weights_name]\n",
" return (df[field_name] * weights).sum() / weights.sum()\n",
"\n",
"\n",
"gdf[\"region\"] = gdf[\"RGIID\"].str[6:8].astype(int)\n",
"grouped = gdf[[\"year\", \"AREA\", \"region\"]].groupby(\"region\")\n",
"arithmetic_year = grouped[\"year\"].mean()\n",
"weighted_year = grouped.apply(weighted_average, \"year\", \"AREA\", include_groups=False)\n",
"\n",
"ax = arithmetic_year.plot(label=\"arithmetic\")\n",
"ax = weighted_year.where(weighted_year != 0).plot(label=\"weighted\")\n",
"ax.set_xlabel(\"RGI region number\")\n",
"ax.set_ylabel(\"Year of digitalization\")\n",
"ax.grid()\n",
"ax.legend()"
]
}
],
"metadata": {
Expand Down

0 comments on commit 2a6dede

Please sign in to comment.