{ "cells": [ { "cell_type": "markdown", "id": "moving-ownership", "metadata": {}, "source": [ "# Margins\n", "\n", " \n", "The margins around the plot and text elements are controlled by the `plot_margin` parameter in `theme()` and the `margin` parameter in `element_text()` respectively.\n", "\n", "Now the parameters `plot_margin` and `margin` accept a number or a list of numbers.\n", "\n", "- A number or list of one number is specified: the same margin it applied to **all four sides**.\n", "- A list of two numbers: the first margin applies to the **top and bottom**, the second - to the **left and right**.\n", "- A list of three numbers: the first margin applies to the **top**, the second - to the **right and left**, the third - to the **bottom**.\n", "- A list of four numbers: the margins are applied to the **top, right, bottom and left** in that order.\n", "\n", "It is acceptable to use None for any side; in this case, the default side value for this element will be used." ] }, { "cell_type": "code", "execution_count": 1, "id": "liable-jacksonville", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "fiscal-rates", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "indie-yeast", "metadata": {}, "outputs": [], "source": [ "np.random.seed(42)\n", "data = {'x': np.random.randint(10, size=100)}\n", "\n", "p = ggplot(data, aes(x='x')) + \\\n", " geom_bar() + \\\n", " ggtitle(\"Bar Сhart\") + \\\n", " theme_light() + \\\n", " theme(plot_background=element_rect(size = 4))" ] }, { "cell_type": "markdown", "id": "cooked-sender", "metadata": {}, "source": [ "#### Plot without Margins" ] }, { "cell_type": "code", "execution_count": 4, "id": "buried-birth", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p" ] }, { "cell_type": "markdown", "id": "brutal-sword", "metadata": {}, "source": [ "#### Margins Around the Plot" ] }, { "cell_type": "code", "execution_count": 5, "id": "functional-client", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + theme(plot_margin=40) + ggtitle(\"plot_margin=40 (all sides)\"),\n", " p + theme(plot_margin=[40,20]) + ggtitle(\"plot_margin=[40,20]\\n(top/bottom, left/right)\"),\n", " p + theme(plot_margin=[40,20,10]) + ggtitle(\"plot_margin=[40,20,10]\\n(top, left/right, bottom)\"),\n", " p + theme(plot_margin=[40,10,10,20]) + ggtitle(\"plot_margin=[40,10,10,20]\\n(top, right, bottom, left)\")\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "interstate-rover", "metadata": {}, "source": [ "#### Margins Around the Text Element" ] }, { "cell_type": "code", "execution_count": 6, "id": "placed-aerospace", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + theme(plot_title = element_text(margin=[40,None]),\n", " axis_title = element_text(margin=[20,20,None,None])) + \\\n", " ggtitle(\"plot_title=[40,None] -> top/bottom=40,\\n\" +\n", " \"axis_title=[20,20,None,None] -> top/right=20\")" ] } ], "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.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }