{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Oceanographic Profiles and T-S Diagrams\n\nThis example demonstrates how to plot vertical profiles of different\nvariables in the same axes, and how to make a scatter plot of two\nvariables. There is an oceanographic theme but the same techniques are\nequally applicable to atmospheric or other kinds of data.\n\nThe data used are profiles of potential temperature and salinity in the\nEquatorial and South Atlantic, output from an ocean model.\n\nThe y-axis of the first plot produced will be automatically inverted due to the\npresence of the attribute positive=down on the depth coordinate. This means\ndepth values intuitively increase downward on the y-axis.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nimport iris\nimport iris.iterate\nimport iris.plot as iplt\n\n\ndef main():\n    # Load the gridded temperature and salinity data.\n    fname = iris.sample_data_path(\"atlantic_profiles.nc\")\n    cubes = iris.load(fname)\n    (theta,) = cubes.extract(\"sea_water_potential_temperature\")\n    (salinity,) = cubes.extract(\"sea_water_practical_salinity\")\n\n    # Extract profiles of temperature and salinity from a particular point in\n    # the southern portion of the domain, and limit the depth of the profile\n    # to 1000m.\n    lon_cons = iris.Constraint(longitude=330.5)\n    lat_cons = iris.Constraint(latitude=lambda l: -10 < l < -9)\n    depth_cons = iris.Constraint(depth=lambda d: d <= 1000)\n    theta_1000m = theta.extract(depth_cons & lon_cons & lat_cons)\n    salinity_1000m = salinity.extract(depth_cons & lon_cons & lat_cons)\n\n    # Plot these profiles on the same set of axes. Depth is automatically\n    # recognised as a vertical coordinate and placed on the y-axis.\n    # The first plot is in the default axes. We'll use the same color for the\n    # curve and its axes/tick labels.\n    plt.figure(figsize=(5, 6))\n    temperature_color = (0.3, 0.4, 0.5)\n    ax1 = plt.gca()\n    iplt.plot(\n        theta_1000m,\n        linewidth=2,\n        color=temperature_color,\n        alpha=0.75,\n    )\n    ax1.set_xlabel(\"Potential Temperature / K\", color=temperature_color)\n    ax1.set_ylabel(\"Depth / m\")\n    for ticklabel in ax1.get_xticklabels():\n        ticklabel.set_color(temperature_color)\n\n    # To plot salinity in the same axes we use twiny(). We'll use a different\n    # color to identify salinity.\n    salinity_color = (0.6, 0.1, 0.15)\n    ax2 = plt.gca().twiny()\n    iplt.plot(\n        salinity_1000m,\n        linewidth=2,\n        color=salinity_color,\n        alpha=0.75,\n    )\n    ax2.set_xlabel(\"Salinity / PSU\", color=salinity_color)\n    for ticklabel in ax2.get_xticklabels():\n        ticklabel.set_color(salinity_color)\n    plt.tight_layout()\n    iplt.show()\n\n    # Now plot a T-S diagram using scatter. We'll use all the profiles here,\n    # and each point will be coloured according to its depth.\n    plt.figure(figsize=(6, 6))\n    depth_values = theta.coord(\"depth\").points\n    for s, t in iris.iterate.izip(salinity, theta, coords=\"depth\"):\n        iplt.scatter(s, t, c=depth_values, marker=\"+\", cmap=\"RdYlBu_r\")\n    ax = plt.gca()\n    ax.set_xlabel(\"Salinity / PSU\")\n    ax.set_ylabel(\"Potential Temperature / K\")\n    cb = plt.colorbar(orientation=\"horizontal\")\n    cb.set_label(\"Depth / m\")\n    plt.tight_layout()\n    iplt.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
}