{ "cells": [ { "cell_type": "markdown", "id": "4a4a06ee-e0ed-44a9-849b-ed6a140b3a55", "metadata": {}, "source": [ "# String Values of Legend Justification\n", "\n", "The `legend_justification` parameter of `theme()` accepts the following string values: `'center'` (the default), `'top'`, `'right'`, `'bottom'`, and `'left'`." ] }, { "cell_type": "code", "execution_count": 1, "id": "a4abc978-2120-48f9-8065-01e15fd058b6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "efdc411b-630d-42c3-966e-5066f375371e", "metadata": {}, "outputs": [], "source": [ "data = {'x': [0, 0.4, 1], 'y': [0, 0.6, 1], 'Color': ['A', 'B', 'C']}\n", "p = ggplot(data, aes('x', 'y', color='Color')) + \\\n", " geom_point() + \\\n", " theme(plot_title=element_text(hjust=0.5)) + \\\n", " ggsize(500, 300)" ] }, { "cell_type": "code", "execution_count": 3, "id": "016c234c-2ffd-496e-a4be-270bbd7fbab9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The base plot with the legend in the center\n", "\n", "p + ggtitle(\"Default legend position\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "4bea670c-3439-4e8d-b685-3ece120998a3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Align the legend to the top or bottom\n", "\n", "gggrid([\n", " p + theme(legend_justification=\"top\") + \n", " ggtitle(\"Align legend to the top\"),\n", " #\n", " p + theme(legend_justification=\"bottom\") + \n", " ggtitle(\"Align legend to the bottom\")])\n", " " ] }, { "cell_type": "code", "execution_count": 5, "id": "d625d5aa-fecd-4f23-8f69-0b0ef816d2f1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# When the legend is placed at the top or bottom, you can apply left or right justification\n", "\n", "gggrid([\n", " p + theme(legend_position=\"bottom\", legend_justification=\"left\") + \n", " ggtitle(\"Align legend to the left\"),\n", " #\n", " p + theme(legend_position=\"bottom\", legend_justification=\"right\") + \n", " ggtitle(\"Align legend to the right\")])" ] } ], "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.23" } }, "nbformat": 4, "nbformat_minor": 5 }