|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "ddddb250-faff-496c-8f0e-6935196ec14a", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Text and Annotations\n", |
| 9 | + "\n", |
| 10 | + "\n", |
| 11 | + "```{note}\n", |
| 12 | + "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", |
| 13 | + "```" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "id": "ec2f6996-5f39-4009-a94d-e3a3fda108d9", |
| 20 | + "metadata": { |
| 21 | + "tags": [] |
| 22 | + }, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "%matplotlib ipympl\n", |
| 26 | + "import matplotlib.pyplot as plt\n", |
| 27 | + "import numpy as np\n", |
| 28 | + "\n", |
| 29 | + "from mpl_interactions import ipyplot as iplt" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "id": "692989d9-07f1-4969-81d8-fc330f0aa5d4", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Working with text strings.\n", |
| 38 | + "\n", |
| 39 | + "There are two ways to dynamically update text strings in mpl-interactions.\n", |
| 40 | + "1. Use a function to return a string\n", |
| 41 | + "2. Use a named string formatting\n", |
| 42 | + "\n", |
| 43 | + "\n", |
| 44 | + "You can also combine these and have your function return a string that then gets formatted.\n", |
| 45 | + "\n", |
| 46 | + "\n", |
| 47 | + "In the example below the `xlabel` is generated using a function and the `title` is generated using the formatting approach." |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "id": "d97390af-872e-42f5-a939-03b734b1cf4f", |
| 54 | + "metadata": { |
| 55 | + "tags": [] |
| 56 | + }, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "fig, ax = plt.subplots()\n", |
| 60 | + "\n", |
| 61 | + "x = np.linspace(0, np.pi, 100)\n", |
| 62 | + "\n", |
| 63 | + "\n", |
| 64 | + "def y(x, volts, tau):\n", |
| 65 | + " return np.sin(x * tau) * volts\n", |
| 66 | + "\n", |
| 67 | + "\n", |
| 68 | + "ctrls = iplt.plot(x, y, volts=(0.5, 10), tau=(1, 10, 100))\n", |
| 69 | + "\n", |
| 70 | + "\n", |
| 71 | + "def xlabel_func(tau):\n", |
| 72 | + " # you can do arbitrary python here to make a more\n", |
| 73 | + " # complicated string\n", |
| 74 | + " return f\"Time with a max tau of {np.round(tau, 3)}\"\n", |
| 75 | + "\n", |
| 76 | + "\n", |
| 77 | + "with ctrls[\"tau\"]:\n", |
| 78 | + " iplt.xlabel(xlabel_func)\n", |
| 79 | + "with ctrls:\n", |
| 80 | + " # directly using string formatting\n", |
| 81 | + " # the formatting is performed in the update\n", |
| 82 | + " iplt.title(title=\"The voltage is {volts:.2f}\")" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "id": "1e152d5d-6c6f-4e87-b5d6-f6755f4bed17", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "## Arbitrarily placed text\n", |
| 91 | + "\n", |
| 92 | + "For this you can use {func}`.interactive_text`. Currently `plt.annotation` is not supported. \n" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "id": "1ccdd8c4-91fa-440a-9a2d-dbea694ee92d", |
| 99 | + "metadata": { |
| 100 | + "tags": [] |
| 101 | + }, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "fig, ax = plt.subplots()\n", |
| 105 | + "\n", |
| 106 | + "theta = np.linspace(0, 2 * np.pi, 100)\n", |
| 107 | + "\n", |
| 108 | + "\n", |
| 109 | + "def gen_string(theta):\n", |
| 110 | + " return f\"angle = {np.round(np.rad2deg(theta))}\"\n", |
| 111 | + "\n", |
| 112 | + "\n", |
| 113 | + "def fx(theta):\n", |
| 114 | + " return np.cos(theta)\n", |
| 115 | + "\n", |
| 116 | + "\n", |
| 117 | + "def fy(x, theta):\n", |
| 118 | + " return np.sin(theta)\n", |
| 119 | + "\n", |
| 120 | + "\n", |
| 121 | + "ctrls = iplt.text(fx, fy, gen_string, theta=theta)\n", |
| 122 | + "ax.set_xlim([-1.25, 1.25])\n", |
| 123 | + "_ = ax.set_ylim([-1.25, 1.25])" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "id": "e2aafbc0-7958-410e-a3b8-ce0a8a75ef30", |
| 129 | + "metadata": { |
| 130 | + "jp-MarkdownHeadingCollapsed": true, |
| 131 | + "tags": [] |
| 132 | + }, |
| 133 | + "source": [ |
| 134 | + "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" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "id": "8eb7da4d-3e48-4b28-85a8-080ff85eee0d", |
| 141 | + "metadata": { |
| 142 | + "tags": [] |
| 143 | + }, |
| 144 | + "outputs": [], |
| 145 | + "source": [ |
| 146 | + "fig, ax = plt.subplots()\n", |
| 147 | + "ctrls = iplt.text((0, 1, 100), (0.25, 1, 100), \"{x:.2f}, {y:.2f}\")" |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "metadata": { |
| 152 | + "kernelspec": { |
| 153 | + "display_name": "Python 3 (ipykernel)", |
| 154 | + "language": "python", |
| 155 | + "name": "python3" |
| 156 | + }, |
| 157 | + "language_info": { |
| 158 | + "codemirror_mode": { |
| 159 | + "name": "ipython", |
| 160 | + "version": 3 |
| 161 | + }, |
| 162 | + "file_extension": ".py", |
| 163 | + "mimetype": "text/x-python", |
| 164 | + "name": "python", |
| 165 | + "nbconvert_exporter": "python", |
| 166 | + "pygments_lexer": "ipython3", |
| 167 | + "version": "3.9.9" |
| 168 | + } |
| 169 | + }, |
| 170 | + "nbformat": 4, |
| 171 | + "nbformat_minor": 5 |
| 172 | +} |
0 commit comments