{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Curvilinear grid demo\n\nCustom grid and ticklines.\n\nThis example demonstrates how to use\n`~.grid_helper_curvelinear.GridHelperCurveLinear` to define custom grids and\nticklines by applying a transformation on the grid.  This can be used, as\nshown on the second plot, to create polar projections in a rectangular box.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.projections import PolarAxes\nfrom matplotlib.transforms import Affine2D\nfrom mpl_toolkits.axisartist import Axes, HostAxes, angle_helper\nfrom mpl_toolkits.axisartist.grid_helper_curvelinear import GridHelperCurveLinear\n\n\ndef curvelinear_test1(fig):\n    \"\"\"\n    Grid for custom transform.\n    \"\"\"\n\n    def tr(x, y): return x, y - x\n    def inv_tr(x, y): return x, y + x\n\n    grid_helper = GridHelperCurveLinear((tr, inv_tr))\n\n    ax1 = fig.add_subplot(1, 2, 1, axes_class=Axes, grid_helper=grid_helper)\n    # ax1 will have ticks and gridlines defined by the given transform (+\n    # transData of the Axes).  Note that the transform of the Axes itself\n    # (i.e., transData) is not affected by the given transform.\n    xx, yy = tr(np.array([3, 6]), np.array([5, 10]))\n    ax1.plot(xx, yy)\n\n    ax1.set_aspect(1)\n    ax1.set_xlim(0, 10)\n    ax1.set_ylim(0, 10)\n\n    ax1.axis[\"t\"] = ax1.new_floating_axis(0, 3)\n    ax1.axis[\"t2\"] = ax1.new_floating_axis(1, 7)\n    ax1.grid(True, zorder=0)\n\n\ndef curvelinear_test2(fig):\n    \"\"\"\n    Polar projection, but in a rectangular box.\n    \"\"\"\n\n    # PolarAxes.PolarTransform takes radian. However, we want our coordinate\n    # system in degree\n    tr = Affine2D().scale(np.pi/180, 1) + PolarAxes.PolarTransform()\n    # Polar projection, which involves cycle, and also has limits in\n    # its coordinates, needs a special method to find the extremes\n    # (min, max of the coordinate within the view).\n    extreme_finder = angle_helper.ExtremeFinderCycle(\n        nx=20, ny=20,  # Number of sampling points in each direction.\n        lon_cycle=360, lat_cycle=None,\n        lon_minmax=None, lat_minmax=(0, np.inf),\n    )\n    # Find grid values appropriate for the coordinate (degree, minute, second).\n    grid_locator1 = angle_helper.LocatorDMS(12)\n    # Use an appropriate formatter.  Note that the acceptable Locator and\n    # Formatter classes are a bit different than that of Matplotlib, which\n    # cannot directly be used here (this may be possible in the future).\n    tick_formatter1 = angle_helper.FormatterDMS()\n\n    grid_helper = GridHelperCurveLinear(\n        tr, extreme_finder=extreme_finder,\n        grid_locator1=grid_locator1, tick_formatter1=tick_formatter1)\n    ax1 = fig.add_subplot(\n        1, 2, 2, axes_class=HostAxes, grid_helper=grid_helper)\n\n    # make ticklabels of right and top axis visible.\n    ax1.axis[\"right\"].major_ticklabels.set_visible(True)\n    ax1.axis[\"top\"].major_ticklabels.set_visible(True)\n    # let right axis shows ticklabels for 1st coordinate (angle)\n    ax1.axis[\"right\"].get_helper().nth_coord_ticks = 0\n    # let bottom axis shows ticklabels for 2nd coordinate (radius)\n    ax1.axis[\"bottom\"].get_helper().nth_coord_ticks = 1\n\n    ax1.set_aspect(1)\n    ax1.set_xlim(-5, 12)\n    ax1.set_ylim(-5, 10)\n\n    ax1.grid(True, zorder=0)\n\n    # A parasite Axes with given transform\n    ax2 = ax1.get_aux_axes(tr)\n    # note that ax2.transData == tr + ax1.transData\n    # Anything you draw in ax2 will match the ticks and grids of ax1.\n    ax2.plot(np.linspace(0, 30, 51), np.linspace(10, 10, 51), linewidth=2)\n\n    ax2.pcolor(np.linspace(0, 90, 4), np.linspace(0, 10, 4),\n               np.arange(9).reshape((3, 3)))\n    ax2.contour(np.linspace(0, 90, 4), np.linspace(0, 10, 4),\n                np.arange(16).reshape((4, 4)), colors=\"k\")\n\n\nif __name__ == \"__main__\":\n    fig = plt.figure(figsize=(7, 4))\n\n    curvelinear_test1(fig)\n    curvelinear_test2(fig)\n\n    plt.show()"
      ]
    }
  ],
  "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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}