{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ddddb250-faff-496c-8f0e-6935196ec14a",
   "metadata": {},
   "source": [
    "# Text and Annotations\n",
    "\n",
    "\n",
    "```{note}\n",
    "Support for modifying text is not complete as none of the function implemented support updating `fontdict` or other text properties like size and color. However, the core functionality is there to place text, change it's position, or change what it reads. see https://github.com/ianhi/mpl-interactions/issues/247 for updates.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec2f6996-5f39-4009-a94d-e3a3fda108d9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%matplotlib ipympl\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from mpl_interactions import ipyplot as iplt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "692989d9-07f1-4969-81d8-fc330f0aa5d4",
   "metadata": {},
   "source": [
    "## Working with text strings.\n",
    "\n",
    "There are two ways to dynamically update text strings in mpl-interactions.\n",
    "1. Use a function to return a string\n",
    "2. Use a named string formatting\n",
    "\n",
    "\n",
    "You can also combine these and have your function return a string that then gets formatted.\n",
    "\n",
    "\n",
    "In the example below the `xlabel` is generated using a function and the `title` is generated using the formatting approach."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d97390af-872e-42f5-a939-03b734b1cf4f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "\n",
    "x = np.linspace(0, np.pi, 100)\n",
    "\n",
    "\n",
    "def y(x, volts, tau):\n",
    "    return np.sin(x * tau) * volts\n",
    "\n",
    "\n",
    "ctrls = iplt.plot(x, y, volts=(0.5, 10), tau=(1, 10, 100))\n",
    "\n",
    "\n",
    "def xlabel_func(tau):\n",
    "    # you can do arbitrary python here to make a more\n",
    "    # complicated string\n",
    "    return f\"Time with a max tau of {np.round(tau, 3)}\"\n",
    "\n",
    "\n",
    "with ctrls[\"tau\"]:\n",
    "    iplt.xlabel(xlabel_func)\n",
    "with ctrls:\n",
    "    # directly using string formatting\n",
    "    # the formatting is performed in the update\n",
    "    iplt.title(title=\"The voltage is {volts:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e152d5d-6c6f-4e87-b5d6-f6755f4bed17",
   "metadata": {},
   "source": [
    "## Arbitrarily placed text\n",
    "\n",
    "For this you can use {func}`.interactive_text`. Currently `plt.annotation` is not supported. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ccdd8c4-91fa-440a-9a2d-dbea694ee92d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "\n",
    "theta = np.linspace(0, 2 * np.pi, 100)\n",
    "\n",
    "\n",
    "def gen_string(theta):\n",
    "    return f\"angle = {np.round(np.rad2deg(theta))}\"\n",
    "\n",
    "\n",
    "def fx(theta):\n",
    "    return np.cos(theta)\n",
    "\n",
    "\n",
    "def fy(x, theta):\n",
    "    return np.sin(theta)\n",
    "\n",
    "\n",
    "ctrls = iplt.text(fx, fy, gen_string, theta=theta)\n",
    "ax.set_xlim([-1.25, 1.25])\n",
    "_ = ax.set_ylim([-1.25, 1.25])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2aafbc0-7958-410e-a3b8-ce0a8a75ef30",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "Since the `x` and `y` positions are scalars you can also do nifty things like directly define them by a slider shorthand in the function.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8eb7da4d-3e48-4b28-85a8-080ff85eee0d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ctrls = iplt.text((0, 1, 100), (0.25, 1, 100), \"{x:.2f}, {y:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
