{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Zonal Mean Diagram of Air Temperature\nThis example demonstrates aligning a linear plot and a cartographic plot using Matplotlib.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import cartopy.crs as ccrs\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\nimport numpy as np\n\nimport iris\nfrom iris.analysis import MEAN\nimport iris.plot as iplt\nimport iris.quickplot as qplt\n\n\ndef main():\n\n    # Loads air_temp.pp and \"collapses\" longitude into a single, average value.\n    fname = iris.sample_data_path(\"air_temp.pp\")\n    temperature = iris.load_cube(fname)\n    collapsed_temp = temperature.collapsed(\"longitude\", MEAN)\n\n    # Set y-axes with -90 and 90 limits and steps of 15 per tick.\n    start, stop, step = -90, 90, 15\n    yticks = np.arange(start, stop + step, step)\n    ylim = [start, stop]\n\n    # Plot \"temperature\" on a cartographic plot and set the ticks and titles\n    # on the axes.\n    fig = plt.figure(figsize=[12, 4])\n\n    ax1 = fig.add_subplot(111, projection=ccrs.PlateCarree())\n    im = iplt.contourf(temperature, cmap=\"RdYlBu_r\")\n    ax1.coastlines()\n    ax1.gridlines()\n    ax1.set_xticks([-180, -90, 0, 90, 180])\n    ax1.set_yticks(yticks)\n    ax1.set_title(\"Air Temperature\")\n    ax1.set_ylabel(f\"Latitude / {temperature.coord('latitude').units}\")\n    ax1.set_xlabel(f\"Longitude / {temperature.coord('longitude').units}\")\n    ax1.set_ylim(*ylim)\n\n    # Create a Matplotlib AxesDivider object to allow alignment of other\n    # Axes objects.\n    divider = make_axes_locatable(ax1)\n\n    # Gives the air temperature bar size, colour and a title.\n    ax2 = divider.new_vertical(\n        size=\"5%\", pad=0.5, axes_class=plt.Axes, pack_start=True\n    )  # creates 2nd axis\n    fig.add_axes(ax2)\n    cbar = plt.colorbar(\n        im, cax=ax2, orientation=\"horizontal\"\n    )  # puts colour bar on second axis\n    cbar.ax.set_xlabel(f\"{temperature.units}\")  # labels colour bar\n\n    # Plot \"collapsed_temp\" on the mean graph and set the ticks and titles\n    # on the axes.\n    ax3 = divider.new_horizontal(\n        size=\"30%\", pad=0.4, axes_class=plt.Axes\n    )  # create 3rd axis\n    fig.add_axes(ax3)\n    qplt.plot(\n        collapsed_temp, collapsed_temp.coord(\"latitude\")\n    )  # plots temperature collapsed over longitude against latitude\n    ax3.axhline(0, color=\"k\", linewidth=0.5)\n\n    # Creates zonal mean details\n    ax3.set_title(\"Zonal Mean\")\n    ax3.yaxis.set_label_position(\"right\")\n    ax3.yaxis.tick_right()\n    ax3.set_yticks(yticks)\n    ax3.grid()\n\n    # Round each tick for the third ax to the nearest 20 (ready for use).\n    data_max = collapsed_temp.data.max()\n    x_max = data_max - data_max % -20\n    data_min = collapsed_temp.data.min()\n    x_min = data_min - data_min % 20\n    ax3.set_xlim(x_min, x_max)\n    ax3.set_ylim(*ylim)\n\n    plt.show()\n\n\nif __name__ == \"__main__\":\n    main()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}