{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Test Data Showing Inset Plots\n\nThis example demonstrates the use of a single 3D data cube with time, latitude\nand longitude dimensions to plot a temperature series for a single latitude\ncoordinate, with an inset plot of the data region.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import cartopy.crs as ccrs\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport iris\nimport iris.plot as iplt\nimport iris.quickplot as qplt\n\n\ndef main():\n    cube1 = iris.load_cube(iris.sample_data_path(\"ostia_monthly.nc\"))\n    # Slice into cube to retrieve data for the inset map showing the\n    # data region\n    region = cube1[-1, :, :]\n    # Average over latitude to reduce cube to 1 dimension\n    plot_line = region.collapsed(\"latitude\", iris.analysis.MEAN)\n\n    # Open a window for plotting\n    fig = plt.figure()\n    # Add a single subplot (axes). Could also use \"ax_main = plt.subplot()\"\n    ax_main = fig.add_subplot(1, 1, 1)\n    # Produce a quick plot of the 1D cube\n    qplt.plot(plot_line)\n\n    # Set x limits to match the data\n    ax_main.set_xlim(0, plot_line.coord(\"longitude\").points.max())\n    # Adjust the y limits so that the inset map won't clash with main plot\n    ax_main.set_ylim(294, 310)\n    ax_main.set_title(\"Meridional Mean Temperature\")\n    # Add grid lines\n    ax_main.grid()\n\n    # Add a second set of axes specifying the fractional coordinates within\n    # the figure with bottom left corner at x=0.55, y=0.58 with width\n    # 0.3 and height 0.25.\n    # Also specify the projection\n    ax_sub = fig.add_axes(\n        [0.55, 0.58, 0.3, 0.25],\n        projection=ccrs.Mollweide(central_longitude=180),\n    )\n\n    # Use iris.plot (iplt) here so colour bar properties can be specified\n    # Also use a sequential colour scheme to reduce confusion for those with\n    # colour-blindness\n    iplt.pcolormesh(region, cmap=\"Blues\")\n    # Manually set the orientation and tick marks on your colour bar\n    ticklist = np.linspace(np.min(region.data), np.max(region.data), 4)\n    plt.colorbar(orientation=\"horizontal\", ticks=ticklist)\n    ax_sub.set_title(\"Data Region\")\n    # Add coastlines\n    ax_sub.coastlines()\n    # request to show entire map, using the colour mesh on the data region only\n    ax_sub.set_global()\n\n    qplt.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.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}