{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Draw flat objects in 3D plot\n\nDemonstrate using `.pathpatch_2d_to_3d` to 'draw' shapes and text on a 3D plot.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Circle, PathPatch\nfrom matplotlib.text import TextPath\nfrom matplotlib.transforms import Affine2D\nimport mpl_toolkits.mplot3d.art3d as art3d\n\n\ndef text3d(ax, xyz, s, zdir=\"z\", size=None, angle=0, usetex=False, **kwargs):\n    \"\"\"\n    Plots the string *s* on the axes *ax*, with position *xyz*, size *size*,\n    and rotation angle *angle*. *zdir* gives the axis which is to be treated as\n    the third dimension. *usetex* is a boolean indicating whether the string\n    should be run through a LaTeX subprocess or not.  Any additional keyword\n    arguments are forwarded to `.transform_path`.\n\n    Note: zdir affects the interpretation of xyz.\n    \"\"\"\n    x, y, z = xyz\n    if zdir == \"y\":\n        xy1, z1 = (x, z), y\n    elif zdir == \"x\":\n        xy1, z1 = (y, z), x\n    else:\n        xy1, z1 = (x, y), z\n\n    text_path = TextPath((0, 0), s, size=size, usetex=usetex)\n    trans = Affine2D().rotate(angle).translate(xy1[0], xy1[1])\n\n    p1 = PathPatch(trans.transform_path(text_path), **kwargs)\n    ax.add_patch(p1)\n    art3d.pathpatch_2d_to_3d(p1, z=z1, zdir=zdir)\n\n\nfig = plt.figure()\nax = fig.add_subplot(projection='3d')\n\n# Draw a circle on the x=0 'wall'\np = Circle((5, 5), 3)\nax.add_patch(p)\nart3d.pathpatch_2d_to_3d(p, z=0, zdir=\"x\")\n\n# Manually label the axes\ntext3d(ax, (4, -2, 0), \"X-axis\", zdir=\"z\", size=.5, usetex=False,\n       ec=\"none\", fc=\"k\")\ntext3d(ax, (12, 4, 0), \"Y-axis\", zdir=\"z\", size=.5, usetex=False,\n       angle=np.pi / 2, ec=\"none\", fc=\"k\")\ntext3d(ax, (12, 10, 4), \"Z-axis\", zdir=\"y\", size=.5, usetex=False,\n       angle=np.pi / 2, ec=\"none\", fc=\"k\")\n\n# Write a Latex formula on the z=0 'floor'\ntext3d(ax, (1, 5, 0),\n       r\"$\\displaystyle G_{\\mu\\nu} + \\Lambda g_{\\mu\\nu} = \"\n       r\"\\frac{8\\pi G}{c^4} T_{\\mu\\nu}  $\",\n       zdir=\"z\", size=1, usetex=True,\n       ec=\"none\", fc=\"k\")\n\nax.set_xlim(0, 10)\nax.set_ylim(0, 10)\nax.set_zlim(0, 10)\n\nplt.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.8.10"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}