{ "cells": [ { "cell_type": "markdown", "id": "046c36f5-dab2-4598-ba2f-4810d9fcf942", "metadata": {}, "source": [ "# LaTeX Fractions\n", "\n", "Lets-Plot now renders LaTeX fractions written as `\\frac{...}{...}` in plot text elements,\n", "such as titles, axis labels, legends, tooltips and `geom_text()` / `geom_label()`.\n", "\n", "**Note:** Nested fractions are not supported." ] }, { "cell_type": "code", "execution_count": 1, "id": "f90758df-8b64-4bb0-acfa-abda56e70408", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "928384f3-43bd-4d9e-af9e-b038b61646a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "e169f9e4-f892-429f-afef-1eae674f21e2", "metadata": {}, "outputs": [], "source": [ "# Define the functions\n", "def f1(x):\n", " return np.sin(3*x) / (x * (np.cos(x) + 2))\n", "\n", "def f2(x):\n", " return np.cos(x) / x\n", "\n", "def f3(x):\n", " return np.exp(-x)" ] }, { "cell_type": "code", "execution_count": 4, "id": "5624bcbb-a24d-4f0b-92fc-cc674c586523", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = ['#FFB6C1', '#AAFFAA', '#ADD8E6']\n", "(ggplot()\n", " + geom_vline(xintercept=0, size=0.4)\n", " + geom_hline(yintercept=0, size=0.4)\n", " + geom_function(fun=f1, xlim=[0.1, 12], color=c[0], size=2,\n", " n=300, manual_key=layer_key(r'\\( \\frac{sin(3x)}{x (cos(x) + 2)} \\)', size=3))\n", " + geom_function(fun=f2, xlim=[0.1, 12], color=c[1],\n", " n=300, manual_key=layer_key(r'\\( \\frac{cos(x)}{x} \\)', size=3))\n", " + geom_function(fun=f3, xlim=[0.1, 12], color=c[2],\n", " n=300, manual_key=layer_key(r'\\( e^{-x} \\)', size=3))\n", " + scale_x_continuous(expand=[0, 0.2])\n", " + scale_y_continuous(limits=[-0.4, 1.0])\n", " + labs(\n", " x='x',\n", " y='f(x)',\n", " title='Mathematical Functions'\n", " )\n", " + theme_classic()\n", "\n", " + theme(\n", " # Titles, labels\n", " text=element_text(face='italic', family='Computer Modern'),\n", " plot_title=element_text(size=20, hjust=0.05),\n", " axis_title=element_text(size=18),\n", " legend_text=element_text(size=18, vjust=0.5),\n", " # Legend\n", " legend_background=element_rect(size=1),\n", " legend_position=[0.95, 0.9],\n", " legend_justification=[1, 1],\n", " # Axis, gridlines\n", " axis_line='blank', axis_ticks='blank',\n", " panel_grid_major=element_line(linetype='longdash'),\n", " )\n", " + theme(\n", " # Layout improvements\n", " panel_inset=4,\n", " plot_inset=0,\n", " axis_text=element_text(margin=0),\n", " axis_title=element_text(margin=0),\n", " plot_margin=16\n", " )\n", "\n", " + flavor_solarized_dark()\n", " + ggsize(900, 500)\n", ")" ] } ], "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.14.3" } }, "nbformat": 4, "nbformat_minor": 5 }