From 2ab483f8550b49b9c5ff5e92c000123382ece32b Mon Sep 17 00:00:00 2001 From: Gedeon Muhawenayo Date: Sun, 22 Sep 2024 11:33:43 -0700 Subject: [PATCH] multi-roi notebook --- notebooks/multi_roi_area_astimate.ipynb | 58 ++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/notebooks/multi_roi_area_astimate.ipynb b/notebooks/multi_roi_area_astimate.ipynb index e1152739..84245143 100644 --- a/notebooks/multi_roi_area_astimate.ipynb +++ b/notebooks/multi_roi_area_astimate.ipynb @@ -121,7 +121,63 @@ "metadata": {}, "outputs": [], "source": [ - "run_area_estimates_config(config, show_output_charts=True)" + "anual_estimates = run_area_estimates_config(config, show_output_charts=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "def plot_crop_area_with_confidence(anual_estimates):\n", + " \"\"\"\n", + " This function takes in a dictionary with crop area data and plots a bar chart \n", + " with 95% confidence intervals for each region and year.\n", + "\n", + " Parameters:\n", + " data (dict): A dictionary where keys are region names and values contain \n", + " crop area data, confidence intervals, and years.\n", + " \"\"\"\n", + " regions = list(anual_estimates.keys())\n", + " years = [2022, 2023]\n", + "\n", + " crop_areas = np.array([anual_estimates[region]['crop area'] for region in regions])\n", + " crop_ci = np.array([anual_estimates[region]['95%CI crop'] for region in regions])\n", + "\n", + " fig, ax = plt.subplots(figsize=(10, 6))\n", + "\n", + " bar_width = 0.35\n", + " index = np.arange(len(regions))\n", + "\n", + " for i, year in enumerate(years):\n", + " ax.bar(index + i * bar_width, crop_areas[:, i], bar_width,\n", + " yerr=crop_ci[:, i], label=f'Year {year}', capsize=5)\n", + "\n", + " ax.set_xlabel('Region')\n", + " ax.set_ylabel('Crop Area (hectares)')\n", + " ax.set_title('Crop Area by Region with 95% Confidence Intervals (2022 vs 2023)')\n", + " ax.set_xticks(index + bar_width / 2)\n", + " ax.set_xticklabels(regions)\n", + " ax.legend()\n", + "\n", + " ax.set_axisbelow(True)\n", + " ax.grid(True, which='both', axis='y', linestyle='--', linewidth=0.7)\n", + "\n", + " plt.tight_layout()\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plot_crop_area_with_confidence(anual_estimates)" ] } ],