{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "saving-matrix", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "rough-following", "metadata": {}, "outputs": [], "source": [ "mpg = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "social-andrews", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg, aes('cty', 'hwy', color='drv')) + geom_point() + labs(color='Drive type') \n", "p" ] }, { "cell_type": "code", "execution_count": 4, "id": "dynamic-delivery", "metadata": {}, "outputs": [], "source": [ "hjust_left = element_text(hjust=0.0)\n", "hjust_middle = element_text(hjust=0.5)\n", "hjust_right = element_text(hjust=1.0)\n", "\n", "h = 420" ] }, { "cell_type": "code", "execution_count": 5, "id": "inappropriate-accountability", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Horizontal justification for plot title, subtitle, caption, legend and axis titles\n", "\n", "def with_hjust(hjust):\n", " return theme(title=hjust, axis_title=hjust, plot_caption=hjust, legend_title=hjust)\n", "\n", "p2 = p + labs(caption='The plot caption')\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p2 + with_hjust(hjust_left) + ggtitle(\"All titles with hjust = 0.0\"), 0, 0)\n", "bunch.add_plot(p2 + with_hjust(hjust_middle) + ggtitle(\"All titles with hjust = 0.5\"), 0, h)\n", "bunch.add_plot(p2 + with_hjust(hjust_right) + ggtitle(\"All titles with hjust = 1.0\"), 0, 2*h)\n", "bunch.show()" ] }, { "cell_type": "code", "execution_count": 6, "id": "defensive-rover", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Vertical justification for legend title\n", "\n", "vjust_bottom = element_text(vjust=0.0)\n", "vjust_center = element_text(vjust=0.5)\n", "vjust_top = element_text(vjust=1.0)\n", "\n", "p3 = p + theme(legend_position='bottom') + scale_color_discrete(guide=guide_legend(ncol=1))\n", "\n", "def with_vjust(vjust):\n", " return theme(legend_title=vjust)\n", "\n", "bunch = GGBunch()\n", "bunch.add_plot(p3 + with_vjust(vjust_bottom) + ggtitle(\"Legend title with vjust = 0.0\"), 0, 0)\n", "bunch.add_plot(p3 + with_vjust(vjust_center) + ggtitle(\"Legend title with vjust = 0.5\"), 0, h)\n", "bunch.add_plot(p3 + with_vjust(vjust_top) + ggtitle(\"Legend title with vjust = 1.0\"), 0, 2*h)\n", "bunch.show()" ] } ], "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.7.13" } }, "nbformat": 4, "nbformat_minor": 5 }