.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/modules/plot_animal_consumption_scaling.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_modules_plot_animal_consumption_scaling.py: ======================================== Reducing UK's animal product consumption ======================================== This example demonstrates how to combine a Food Balance Sheet array with a scaling model to reduce animal product consumption, while keeping total consumption constant across all items. It also employs a few ``fbs`` accessor class functions to group and plot food balance sheet arrays. Consumption of animal based products is halved, while keeping total comsumed weight constant by upscaling the consumption of vegetal products. .. GENERATED FROM PYTHON SOURCE LINES 14-48 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt from agrifoodpy_data.food import FAOSTAT from agrifoodpy.food.model import balanced_scaling # Select food items and production values for the last year of data in the UK # Values are in 1000 Tonnes country_code = 229 food_uk = FAOSTAT.isel(Year=-1).sel(Region=country_code) # Select all Animal Products according to its Item_origin, which is a # non-dimension coordinate animal_items = food_uk.sel(Item=food_uk.Item_origin=="Animal Products").Item.values # Scale domestic use of animal items by a factor of 0.5, while keeping # the sum of domestic use constant. Reduce imports to account for the new # consumption values and use production as fallback, subtracting any negative # excess if required food_uk_scaled = balanced_scaling( food_uk, element="domestic", scale=0.5, items=animal_items, constant=True, origin="imports", fallback="production", add_to_fallback=False ) # We group the original and scaled quantities by origin and plot to compare food_uk_origin = food_uk.fbs.group_sum("Item_origin") food_uk_scaled_origin = food_uk_scaled.fbs.group_sum("Item_origin") .. GENERATED FROM PYTHON SOURCE LINES 49-54 From the plot we can see that domestic use of animal products is reduced by half, while keeping total weight constant. We used ``production`` as the fallback for any extra origin required. If any domestic use reduction requires more origin reduction than available, the remaining is taken from the ``fallback`` DataArray element. .. GENERATED FROM PYTHON SOURCE LINES 54-72 .. code-block:: Python # Plot and compare values before and after f, axes = plt.subplots(2,1, sharex=True) plt.subplots_adjust(hspace=0) food_uk_origin.fbs.plot_bars(show="Item_origin", elements=["production", "imports"], inverted_elements=["exports", "domestic"], labels="show", ax=axes[0]) food_uk_scaled_origin.fbs.plot_bars(show="Item_origin", elements=["production", "imports"], inverted_elements=["exports", "domestic"], labels="show", ax=axes[1]) axes[1].set_xlabel("1000 Tonnes") plt.tight_layout() plt.show() .. image-sg:: /examples/modules/images/sphx_glr_plot_animal_consumption_scaling_001.png :alt: plot animal consumption scaling :srcset: /examples/modules/images/sphx_glr_plot_animal_consumption_scaling_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.847 seconds) .. _sphx_glr_download_examples_modules_plot_animal_consumption_scaling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_animal_consumption_scaling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_animal_consumption_scaling.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_animal_consumption_scaling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_