{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multi-line Labels in Legend\n", "\n", "The 'newline' character (`\\n`) now works as 'line break' in the legend text." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "p = ggplot(mpg, aes(\"displ\", \"hwy\")) + geom_point(aes(color=\"cty\", shape=\"drv\"), size=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Legend labels with 'line breaks'." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cty_breaks = [35,30,25,20,15,10]\n", "cty_labels = [str(br) + \"\\n(mpg)\" for br in cty_breaks]\n", "\n", "drv_breaks = [\"f\", \"r\", \"4\"]\n", "drv_labels = [\"Front-wheel\\ndrive\", \"Rear \\n wheel \\n drive\", \"4 wheel drive\"]\n", "\n", "\n", "p + \\\n", " scale_color_continuous(breaks=cty_breaks, labels=cty_labels) + \\\n", " scale_shape(breaks=drv_breaks, labels=drv_labels) + \\\n", " theme(legend_position=\"bottom\")" ] } ], "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.16" } }, "nbformat": 4, "nbformat_minor": 4 }