{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "531b2116-7af8-450c-88db-bb3257b2e596", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Copy button is now ENABLED.\n" ] } ], "source": [ "%toggle_copy_spec_button on" ] }, { "cell_type": "markdown", "id": "0a47783b-bc0d-47d8-9955-797abb2c0da5", "metadata": {}, "source": [ "# LaTeX" ] }, { "cell_type": "code", "execution_count": 2, "id": "82725896-3714-4a2d-978d-f9c3f08b746a", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 3, "id": "f09caf45-61ae-47f9-a9b5-6618b58d641e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 4, "id": "f309984e-88db-4ddd-8f90-9edaeb315af4", "metadata": {}, "outputs": [], "source": [ "formulas = [\n", " r\"\\(a\\)\",\n", " r\"\\(a \\quad + \\: 1\\)\",\n", " r\"\\( \\alpha \\cdot b + \\gamma \\)\",\n", " r\"\\((a - b)(a + b)\\)\",\n", " r\"\\((a / (\\Delta / c))\\)\",\n", " r\"\\(2^3\\)\",\n", " r\"\\(\\Alpha + \\alpha^{\\Beta + \\beta^{\\Gamma + \\gamma_{\\Delta + \\delta + \\Delta} + \\Gamma} + \\Beta} + \\Alpha\\)\",\n", " r\"t\\(^2\\)\",\n", " r\"\\((\\eta + a^{d})(\\eta - a^d)\\)\",\n", " r\"\\(a_2 x^2\\)\",\n", " r\"\\(\\Alpha\\Beta_{\\gamma\\delta}\\Sigma^\\infty\\)\",\n", " r\"\\((1 + (a^2 + 2))(b^{2\\theta - 1} - (c^{-1} / \\chi_i))\\)\",\n", " r\"\\(\\frac{a}{b}\\)\",\n", " r\"\\(a + \\frac{b}{c}\\)\",\n", " r\"\\(\\frac{a}{b} + c\\)\",\n", " r\"\\(\\frac{a}{b} + \\frac{c}{d}\\)\",\n", " r\"\\(\\frac{a + b}{d} + \\frac{d}{e + f}\\)\",\n", " r\"\\(\\frac{a}{b + c} + \\frac{d + e}{f}\\)\",\n", " r\"\\(10^{1000} - \\frac{\\Alpha^\\beta - \\gamma_\\Delta}{\\Omega} - \\frac{\\omega}{\\alpha_\\Beta - \\Gamma^\\delta} - 1000^{10}\\)\",\n", " r\"\\(\\alpha^\\frac{a + b}{c} + \\beta_{\\frac{d}{e + f}}\\)\",\n", " r\"\\(A^{\\frac{a^2}{b_1 + c_1} + \\frac{d_1 + e_1}{f^2}} + \\frac{B + C^2}{D^2 + E}\\)\",\n", " r\"\\(1^{2^{\\frac{3^4 + 3}{3^4 + 3} + 3} + 2} + 1^{2^{\\frac{3^4 + 3}{3^4 + 3} + 3} + 2}\\)\",\n", " r\"ABC & \\(a + \\frac{b + c + d}{e} + f\\) & DEF\",\n", " r'GitHub & \\(a + \\frac{b + c + d}{e} + f\\) & GitHub',\n", "]\n", "additional_formulas = [\n", " r\"*Markdown* & \\(a + \\frac{b + c + d}{e} + f\\) & *Markdown*\",\n", " r\"**Markdown** & \\(a + \\frac{b + c + d}{e} + f\\) & **Markdown**\",\n", " r\"Markdown & \\(a + \\frac{b + c + d}{e} + f\\) & Markdown\",\n", " r\"\\(a + \\frac{b + c + d}{e} + f\\)\" + ' \\n' + r\"\\(a + \\frac{b + c + d}{e} + f\\)\",\n", "]" ] }, { "cell_type": "code", "execution_count": 5, "id": "9e89304b-9270-405c-bcdf-cfbe265da326", "metadata": {}, "outputs": [], "source": [ "def formula_to_plot1(formula, n=None, hjust=None, nudge_x=None, livemap=False):\n", " p = ggplot()\n", " if livemap:\n", " p += geom_livemap()\n", " p += geom_hline(yintercept=0, color=\"white\", tooltips='none') + \\\n", " geom_vline(xintercept=0, color=\"white\", tooltips='none') + \\\n", " geom_text(x=0, label=formula, size=10, color=\"blue\",\n", " hjust=hjust, nudge_x=nudge_x) + \\\n", " theme_void() + \\\n", " theme(plot_background=element_rect(fill=\"#eeeeee\", size=2),\n", " plot_title=element_markdown(size=10))\n", " if n is not None:\n", " latex = formula.replace(r\"\\(\", \"$\").replace(r\"\\)\", \"$\")\n", " p += ggtitle('**LaTeX #{0}:** \"{1}\"'.format(n, latex.replace(\"^\", r\"\\^\")))\n", " return p\n", "\n", "def formula_to_plot2(formula, n=None, hjust=None, nudge_x=None, livemap=False):\n", " p = ggplot()\n", " if livemap:\n", " p += geom_livemap()\n", " p += geom_hline(aes(yintercept=[0, -.25, .25]), color=\"white\", tooltips='none') + \\\n", " geom_vline(xintercept=0, color=\"white\", tooltips='none') + \\\n", " geom_text(x=0, label=formula, size=1, size_unit='y', color=\"blue\",\n", " hjust=hjust, nudge_x=nudge_x) + \\\n", " ylim(-.6, .6) + \\\n", " theme_void() + \\\n", " theme(plot_background=element_rect(fill=\"#eeeeee\", size=2),\n", " plot_title=element_markdown(size=10))\n", " if n is not None:\n", " latex = formula.replace(r\"\\(\", \"$\").replace(r\"\\)\", \"$\")\n", " p += ggtitle('**LaTeX #{0}:** \"{1}\"'.format(n, latex.replace(\"^\", r\"\\^\")))\n", " return p\n", "\n", "def formula_to_plot3(formula, n=None, hjust=None, **args):\n", " caption = None\n", " if n is not None:\n", " latex = formula.replace(r\"\\(\", \"$\").replace(r\"\\)\", \"$\")\n", " caption = '**LaTeX #{0}:** \"{1}\"'.format(n, latex.replace(\"^\", r\"\\^\"))\n", " return ggplot() + \\\n", " geom_point(x=0, color=\"rgba(0,0,0,0)\", tooltips='none') + \\\n", " theme_void() + \\\n", " labs(title=formula, caption=caption) + \\\n", " theme(plot_background=element_rect(fill=\"#eeeeee\", size=2),\n", " plot_title=element_markdown(size=20, margin=[20, 0, 0, 0], hjust=hjust, color=\"blue\"),\n", " plot_caption=element_markdown(hjust=0))\n", "\n", "def formulas_plot(plot_function, formulas_list=formulas, width=1000, height=1500, **args):\n", " return gggrid([\n", " plot_function(formula, n=i, **args)\n", " for i, formula in enumerate(formulas_list)\n", " ], ncol=2) + ggsize(width, height)" ] }, { "cell_type": "markdown", "id": "43b46a21-f52f-4c46-8bb4-01fcfacf3818", "metadata": {}, "source": [ "### Centered `hjust`" ] }, { "cell_type": "code", "execution_count": 6, "id": "30c24b13-c401-4860-9659-1472c08987c8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot1)" ] }, { "cell_type": "code", "execution_count": 7, "id": "41313626-3163-4483-ab60-6dbe5bb2b7d4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot2)" ] }, { "cell_type": "code", "execution_count": 8, "id": "438c52bf-cb6c-48d0-9924-0981aa1543c2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot3, formulas_list=formulas + additional_formulas, height=2300, hjust=0.5)" ] }, { "cell_type": "markdown", "id": "1b7a7225-0e9c-432a-9eb8-08dc26fc1aff", "metadata": {}, "source": [ "### `hjust='left'`" ] }, { "cell_type": "code", "execution_count": 9, "id": "168bdad3-ac60-4d3a-b636-5931afdd8402", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot1, hjust='left', nudge_x=-.5)" ] }, { "cell_type": "code", "execution_count": 10, "id": "5555e9e2-ba3f-4c17-ab9c-9a9c8f4c44a2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot3, formulas_list=formulas + additional_formulas, height=2300, hjust=.1)" ] }, { "cell_type": "markdown", "id": "234ab01d-8f9f-4527-899c-b6408fd27564", "metadata": {}, "source": [ "### `hjust='right'`" ] }, { "cell_type": "code", "execution_count": 11, "id": "1420a0cc-00de-4358-ae4d-13b54845c360", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot1, hjust='right', nudge_x=.5)" ] }, { "cell_type": "code", "execution_count": 12, "id": "8822fab4-b7f4-4783-a78a-4eca8cb36d68", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formulas_plot(formula_to_plot3, formulas_list=formulas + additional_formulas, height=2300, hjust=.6)" ] }, { "cell_type": "markdown", "id": "01966d80-2099-4a71-80f1-d368b91d3f16", "metadata": {}, "source": [ "### Line heights" ] }, { "cell_type": "code", "execution_count": 13, "id": "612d4f36-9b06-4dd3-9233-ab3af9008a6e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_bin_digits(n):\n", " return [\n", " tuple([0]*(n - len(l)) + l)\n", " for l in [\n", " [int(d) for d in bin(v).replace('0b', '')]\n", " for v in range(2**n)\n", " ]\n", " ]\n", "\n", "def get_labels(n):\n", " labels = [r\"\\( A_[] \\)\", r\"\\( \\frac{ B_[] }{ C_[] } \\)\"]\n", " return [\n", " \"\\n\".join([labels[d].replace(\"[]\", \"{ \" + str(i + 1) + \" }\") for i, d in enumerate(digits)])\n", " for digits in get_bin_digits(n)\n", " ]\n", "\n", "def labels_plot(labels, ncol):\n", " data = {\n", " 'x': [i % ncol for i in range(len(labels))],\n", " 'y': [-int(i / ncol) for i in range(len(labels))],\n", " 'l': labels,\n", " }\n", " return ggplot(data, aes('x', 'y')) + \\\n", " geom_label(aes(label='l')) + \\\n", " xlim(-.25, ncol - .75) + ylim(.25, -int((len(labels) - 1) / ncol) - .25)\n", "\n", "labels = (get_labels(2) + get_labels(3) + get_labels(4) + get_labels(5))[:]\n", "labels_plot(labels, ncol=20) + ggsize(1000, 800)" ] }, { "cell_type": "code", "execution_count": 14, "id": "9fbabcee-3049-49e0-a82a-8bc3b4f51aa6", "metadata": {}, "outputs": [], "source": [ "def vertically_aligned_labels_plot(vjust, use_frac, lineheight=None):\n", " if use_frac:\n", " label = \"A\\n\\\\( \\\\frac{B}{C} \\\\)\"\n", " else:\n", " label = \"A\\nB\"\n", " return ggplot() + \\\n", " geom_label(y=1, label=label, lineheight=lineheight, vjust=vjust) + \\\n", " geom_point(y=1, color=\"red\", shape=3) + \\\n", " ggtitle(\"vjust={0}\".format(vjust))" ] }, { "cell_type": "code", "execution_count": 15, "id": "7232c4db-9395-4514-a671-c26b0cec1723", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " vertically_aligned_labels_plot(None, False),\n", " vertically_aligned_labels_plot(None, True),\n", " vertically_aligned_labels_plot('top', False),\n", " vertically_aligned_labels_plot('top', True),\n", " vertically_aligned_labels_plot('center', False),\n", " vertically_aligned_labels_plot('center', True),\n", " vertically_aligned_labels_plot('bottom', False),\n", " vertically_aligned_labels_plot('bottom', True),\n", " vertically_aligned_labels_plot('inward', False),\n", " vertically_aligned_labels_plot('inward', True),\n", " vertically_aligned_labels_plot('outward', False),\n", " vertically_aligned_labels_plot('outward', True),\n", "], ncol=4)" ] }, { "cell_type": "code", "execution_count": 16, "id": "20598674-9b20-4f22-84e6-25d0917a804c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " vertically_aligned_labels_plot(None, False, lineheight=3),\n", " vertically_aligned_labels_plot(None, True, lineheight=3),\n", " vertically_aligned_labels_plot('top', False, lineheight=3),\n", " vertically_aligned_labels_plot('top', True, lineheight=3),\n", " vertically_aligned_labels_plot('center', False, lineheight=3),\n", " vertically_aligned_labels_plot('center', True, lineheight=3),\n", " vertically_aligned_labels_plot('bottom', False, lineheight=3),\n", " vertically_aligned_labels_plot('bottom', True, lineheight=3),\n", " vertically_aligned_labels_plot('inward', False, lineheight=3),\n", " vertically_aligned_labels_plot('inward', True, lineheight=3),\n", " vertically_aligned_labels_plot('outward', False, lineheight=3),\n", " vertically_aligned_labels_plot('outward', True, lineheight=3),\n", "], ncol=4)" ] }, { "cell_type": "code", "execution_count": 17, "id": "c57af8a8-e952-4c29-8b34-bddcf00a49a6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def vertically_aligned_theme_elements_plot(vjust, text_index=0):\n", " text = [\n", " \"A\",\n", " r\"\\( \\frac{A}{B} \\)\",\n", " r\"\\( A\\frac{B}{C} \\)\",\n", " \"\\\\( \\\\frac{A}{B} \\\\)\\nC\",\n", " \"\\\\( A\\\\frac{B}{C} \\\\)\\nD\",\n", " ][text_index]\n", " return ggplot() + \\\n", " geom_point(aes(x=[text]), tooltips=layer_tooltips([\"x\"])) + \\\n", " scale_x_continuous(text) + \\\n", " scale_y_continuous(text, breaks=[0], labels=[text]) + \\\n", " theme(\n", " title=element_text(vjust=vjust),\n", " axis_title=element_text(vjust=vjust),\n", " axis_text=element_text(vjust=vjust),\n", " tooltip_text=element_text(vjust=vjust),\n", " axis_tooltip_text=element_text(vjust=vjust),\n", " plot_background=element_rect(fill=\"gray80\"),\n", " panel_background=element_rect(fill=\"gray90\"),\n", " ) + \\\n", " ggtitle(\"{0}\\nvjust={1}\".format(text, vjust))\n", "\n", "gggrid([\n", " vertically_aligned_theme_elements_plot(0),\n", " vertically_aligned_theme_elements_plot(0.5),\n", " vertically_aligned_theme_elements_plot(1),\n", " vertically_aligned_theme_elements_plot(0, 1),\n", " vertically_aligned_theme_elements_plot(0.5, 1),\n", " vertically_aligned_theme_elements_plot(1, 1),\n", " vertically_aligned_theme_elements_plot(0, 2),\n", " vertically_aligned_theme_elements_plot(0.5, 2),\n", " vertically_aligned_theme_elements_plot(1, 2),\n", " vertically_aligned_theme_elements_plot(0, 3),\n", " vertically_aligned_theme_elements_plot(0.5, 3),\n", " vertically_aligned_theme_elements_plot(1, 3),\n", " vertically_aligned_theme_elements_plot(0, 4),\n", " vertically_aligned_theme_elements_plot(0.5, 4),\n", " vertically_aligned_theme_elements_plot(1, 4),\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "c0092b4d-b052-4881-aa3b-18ae249c5d18", "metadata": {}, "source": [ "### In label" ] }, { "cell_type": "code", "execution_count": 18, "id": "5753badb-b269-4edd-b395-e8c25631e405", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "label_formula = \"First: \\\\( \\\\Omega \\\\cdot \\\\frac{ 10 - a^{b_1} }{\\\\sigma} \\\\)\\n\" \\\n", " \"Second: \\\\( \\\\Theta_{ a_1 a_2 a_3 a_4 ... a_{n + 1} } \\\\)\\n\" \\\n", " \"Third\\n\" \\\n", " \"Fourth: \\\\( \\\\frac{A_1}{A_2} + \\\\frac{B_1}{B_2} + \\\\frac{C_1}{C_2} + \\\\frac{D_1}{D_2} + \\\\frac{E_1}{E_2} + \\\\frac{F_1}{F_2} \\\\)\\n\" \\\n", " \"Fifth and final one for the moment: \\\\( \\\\frac{\\\\Gamma}{\\\\Delta} \\\\)\"\n", "ggplot() + \\\n", " geom_label(x=0, label=label_formula)" ] }, { "cell_type": "markdown", "id": "66b7da55-a888-4e40-892c-9ef2a3823118", "metadata": {}, "source": [ "### In facet title" ] }, { "cell_type": "code", "execution_count": 19, "id": "3fa0b201-e5ec-4aff-be48-fd7e873196a4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "facet_data = {\n", " 'x': [0, 0],\n", " 'f': [r\"\\( \\frac{A}{B} \\)\", \"C\"]\n", "}\n", "\n", "ggplot(facet_data, aes(x='x')) + \\\n", " geom_point() + \\\n", " facet_grid(x='f') + \\\n", " theme_grey()" ] }, { "cell_type": "markdown", "id": "d51c28c4-cffb-46d0-a0f9-d35fffba758b", "metadata": {}, "source": [ "### In legend" ] }, { "cell_type": "code", "execution_count": 20, "id": "09398d98-65e1-4ad0-b1cb-a3d1056dee2e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "legend_data1 = [\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"\\\\( \\\\frac{A}{B} \\\\)\", \"C\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"A\", \"\\\\( \\\\frac{B}{C} \\\\)\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"\\\\( A\\\\frac{B}{C} \\\\)\", \"D\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"A\", \"\\\\( B\\\\frac{C}{D} \\\\)\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"\\\\( \\\\frac{A}{B} \\\\)\\nC\", \"D\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"A\", \"\\\\( \\\\frac{B}{C} \\\\)\\nD\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"\\\\( A\\\\frac{B}{C} \\\\)\\nD\", \"E\"]\n", " },\n", " {\n", " 'x': [0, 1],\n", " 'l': [\"A\", \"\\\\( B\\\\frac{C}{D} \\\\)\\nE\"]\n", " },\n", "]\n", "\n", "gggrid([\n", " ggplot(data, aes(x='x', color='l')) + \\\n", " geom_point(shape=22) + \\\n", " theme(legend_position=pos) + \\\n", " ggtitle(\"{0} #{1}\".format(pos, i))\n", " for i, data in enumerate(legend_data1)\n", " for pos in ['right', 'bottom']\n", "], ncol=4)" ] }, { "cell_type": "code", "execution_count": 21, "id": "e5bb47c3-7240-4406-8ceb-43442dda45b4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "legend_data2 = {\n", " 'x': list(range(5))\n", "}\n", "\n", "ggplot(legend_data2, aes(x='x', color='x')) + \\\n", " geom_point() + \\\n", " scale_color_continuous(breaks=[0, 1, 2, 3, 4],\n", " labels=[\n", " r\"\\( \\frac{0}{1} \\)\",\n", " \"1\",\n", " r\"\\( \\frac{2}{1} \\)\",\n", " \"3\",\n", " r\"\\( \\frac{4}{1} \\)\",\n", " ])" ] }, { "cell_type": "markdown", "id": "19cc3462-bc99-4307-8617-0e15f704826f", "metadata": {}, "source": [ "### Styles" ] }, { "cell_type": "code", "execution_count": 22, "id": "ed274a01-41e3-4916-8fa8-0d29cfa17bde", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def label_with_style(plot_formula, **options):\n", " return ggplot() + \\\n", " geom_point(x=0, size=10, shape=3) + \\\n", " geom_text(x=0, label=plot_formula, **options) + \\\n", " ggtitle(str(options))\n", "\n", "style_formula = \"First: \\\\( \\\\Omega \\\\cdot \\\\frac{ 10 - a^{b_1} }{\\\\sigma} \\\\) \\n Second: \\\\( \\\\Theta_{ a_1 a_2 ... a_{n + 1} } \\\\)\"\n", "gggrid([\n", " label_with_style(style_formula, alpha=.25),\n", " label_with_style(style_formula, color=\"red\"),\n", " label_with_style(style_formula, fill=\"yellow\"),\n", " label_with_style(style_formula, size=12),\n", " label_with_style(style_formula, family=\"Times\"),\n", " label_with_style(style_formula, fontface=\"italic\"),\n", " label_with_style(style_formula, hjust='right'),\n", " label_with_style(style_formula, vjust='top'),\n", " label_with_style(style_formula, angle=30),\n", " label_with_style(style_formula, lineheight=3),\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "cc04ba82-c4f2-446f-ac98-51884ae66fad", "metadata": {}, "source": [ "### All plot elements" ] }, { "cell_type": "code", "execution_count": 23, "id": "8f942aeb-757f-4b05-886c-388476b8c5d1", "metadata": {}, "outputs": [], "source": [ "def get_plot(plot_formula, *, facet_groups=[\"g1\", \"g2\"], show_label=False):\n", " formula_data = {\"x\": [plot_formula, plot_formula], \"g\": facet_groups}\n", " p = ggplot(formula_data, aes(x=\"x\")) + \\\n", " geom_bar(aes(color=\"g\"), y=1, stat='identity', size=2,\n", " fill=\"black\", labels=layer_labels(['x'])) + \\\n", " scale_x_continuous('X: \"{0}\"'.format(plot_formula)) + \\\n", " scale_y_continuous('Y: \"{0}\"'.format(plot_formula), limits=[0, 3] if show_label else None) + \\\n", " scale_color_discrete(name=plot_formula) + \\\n", " labs(title='Title: \"{0}\"'.format(plot_formula),\n", " subtitle='Subtitle: \"{0}\"'.format(plot_formula),\n", " caption='Caption: \"{0}\"'.format(plot_formula)) + \\\n", " facet_grid(x=\"g\") + \\\n", " theme(legend_position='bottom')\n", " if show_label:\n", " p += geom_label(aes(label=\"x\", color=\"g\"), y=2, tooltips=layer_tooltips().title(\"^x\").line(\"{0}|^x\".format(plot_formula)))\n", " return p" ] }, { "cell_type": "code", "execution_count": 24, "id": "58d7bd81-0e1b-48a7-af16-f36a48e4f8eb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Take a look at the tooltips\n", "get_plot(r\"F: \\( \\Omega \\cdot \\frac{ 10 - a^{b_1} }{\\sigma} \\neq 0 \\) (L)\",\n", " facet_groups=[r\"F1: \\( \\Sigma \\cdot \\frac{ 20 - a^{b_1} }{\\rho} \\geq 1 \\)\",\n", " r\"F2: \\( \\Delta \\cdot \\frac{\\xi}{ 30 - c_{d^2} } \\leq -1 \\)\"],\n", " show_label=True) + \\\n", " ggsize(1000, 600)" ] }, { "cell_type": "code", "execution_count": 25, "id": "2d8e6e48-fe2f-40cd-9f13-2df6bfccbfdc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Take a look at the axis breaks\n", "get_plot(r\"F: \\( \\Omega \\cdot \\frac{ 10 - a^{b_1} }{\\sigma} \\neq 0 \\) (L)\",\n", " facet_groups=[r\"F1: \\( \\Sigma \\cdot \\frac{ 20 - a^{b_1} }{\\rho} \\geq 1 \\)\",\n", " r\"F2: \\( \\Delta \\cdot \\frac{\\xi}{ 30 - c_{d^2} } \\leq -1 \\)\"],\n", " show_label=True) + \\\n", " coord_polar() + \\\n", " ggsize(1000, 600)" ] }, { "cell_type": "code", "execution_count": 26, "id": "442ef127-c302-4794-b10d-845e4dd6adb5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "themed_plot = get_plot(\"\\( a^2 \\)\")\n", "custom_element_text = element_text(color=\"green\", family=\"Times\", face=\"italic\", size=30)\n", "gggrid([\n", " themed_plot,\n", " themed_plot + theme(text=custom_element_text),\n", " themed_plot + theme(plot_title=custom_element_text,\n", " plot_subtitle=custom_element_text,\n", " plot_caption=custom_element_text),\n", " themed_plot + theme(axis_title=custom_element_text),\n", " themed_plot + theme(legend_title=custom_element_text),\n", " themed_plot + theme(tooltip_text=custom_element_text,\n", " axis_tooltip_text=custom_element_text),\n", "], ncol=2) + ggsize(1000, 1000)" ] }, { "cell_type": "code", "execution_count": 27, "id": "cd30d945-d76e-447c-b0ff-21f1f337f1c1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "themed_polar_plot = themed_plot + coord_polar()\n", "gggrid([\n", " themed_polar_plot,\n", " themed_polar_plot + theme(text=custom_element_text),\n", " themed_polar_plot + theme(plot_title=custom_element_text,\n", " plot_subtitle=custom_element_text,\n", " plot_caption=custom_element_text),\n", " themed_polar_plot + theme(axis_title=custom_element_text),\n", " themed_polar_plot + theme(legend_title=custom_element_text),\n", " themed_polar_plot + theme(tooltip_text=custom_element_text,\n", " axis_tooltip_text=custom_element_text),\n", "], ncol=2) + ggsize(1000, 1000)" ] }, { "cell_type": "markdown", "id": "bcd693fd-9bd5-4b54-be66-6ba30564673d", "metadata": {}, "source": [ "### Regressions" ] }, { "cell_type": "code", "execution_count": 28, "id": "bc0a3ac5-8530-42d2-abaf-db9909cb4968", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Horizontal alignment\n", "gggrid([\n", " formula_to_plot1(formulas[13], hjust='left'),\n", " formula_to_plot1(formulas[13], hjust='middle'),\n", " formula_to_plot1(formulas[13], hjust='right'),\n", "])" ] }, { "cell_type": "code", "execution_count": 29, "id": "e65d2d83-cc12-44b8-b91a-f6a878273ee3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Mix LaTeX and plain text\n", "gggrid([\n", " gggrid([\n", " formula_to_plot1(r\"AB-\\(a + \\frac{b + c}{d} + e\\)-CD\", hjust='left'),\n", " formula_to_plot1(r\"AB-\\(a + \\frac{b + c}{d} + e\\)-CD\", hjust='middle'),\n", " formula_to_plot1(r\"AB-\\(a + \\frac{b + c}{d} + e\\)-CD\", hjust='right'),\n", " ]),\n", " gggrid([\n", " formula_to_plot1(r\"AAAAAA\\( \\frac{a + b}{c} + e \\)\", hjust='left'),\n", " formula_to_plot1(r\"\\( AAAAAA\\frac{a + b}{c} + e \\)\", hjust='left'),\n", " formula_to_plot1(r\"------\\( \\frac{a + b}{c} + e \\)\", hjust='left'),\n", " formula_to_plot1(r\"AAAAAAAAAAAA\\( \\frac{a + b}{c} + e \\)\", hjust='left'),\n", " ], ncol=2)\n", "], ncol=1) + ggsize(1000, 600)" ] }, { "cell_type": "code", "execution_count": 30, "id": "4ae2bf60-ef21-4645-aafa-4c774908b1ad", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Mix LaTeX and links\n", "gggrid([\n", " formula_to_plot1(formulas[23], hjust='left'),\n", " formula_to_plot1(formulas[23], hjust='middle'),\n", " formula_to_plot1(formulas[23], hjust='right'),\n", "])" ] }, { "cell_type": "code", "execution_count": 31, "id": "83a11654-866e-47aa-9cc0-919a04565919", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# LaTeX in title\n", "gggrid([\n", " formula_to_plot3(formulas[13], hjust=.1),\n", " formula_to_plot3(formulas[13], hjust=.5),\n", " formula_to_plot3(formulas[13], hjust=.6),\n", "])" ] }, { "cell_type": "code", "execution_count": 32, "id": "6b3ff92a-39de-4676-a7d5-bc4bd2ba57b5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong shift (from b/c)\n", "formula_to_plot1(r\"\\( A^{a + \\frac{b}{c} } \\)\", hjust='left') + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 33, "id": "af80db43-6324-4c1a-bb97-47d1cd26d3e3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong shift (from B/C)\n", "formula_to_plot1(r\"\\( A^{a + \\frac{b}{c} } + \\frac{B}{C} \\)\", hjust='left') + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 34, "id": "3111550b-d928-4cb2-8f56-3622d24ccfe6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong shift (after \\alpha)\n", "formula_to_plot1(formulas[19], hjust='left') + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 35, "id": "4af36961-89e3-4b39-963f-9b0ddb3c1387", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong shift\n", "formula_to_plot1(r\"\\( a^{b^{\\frac{c}{d} + C} + B} + A \\)\", hjust='left') + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 36, "id": "3da8c8a4-7043-42a5-aa26-6fd64ef4127f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong shift (after 1^2)\n", "formula_to_plot1(formulas[21], hjust='left') + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 37, "id": "b084f1d5-1c23-4b29-b015-3dd145042329", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong shift #6 (after \"a+\")\n", "formula_to_plot1(formulas[22], hjust='left') + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 38, "id": "8af6be5b-1f1c-4900-8c9c-b13e2c68316b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Wrong positioning\n", "formula_to_plot3(additional_formulas[3], hjust=0) + ggsize(600, 100)" ] }, { "cell_type": "code", "execution_count": 39, "id": "1675f5b2-40a0-4ff1-9dce-bc6c6f7503ef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Copy button is now DISABLED.\n" ] } ], "source": [ "%toggle_copy_spec_button off" ] } ], "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.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }