{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "round-logistics", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:24.947776Z", "iopub.status.busy": "2024-04-17T07:29:24.947501Z", "iopub.status.idle": "2024-04-17T07:29:25.261167Z", "shell.execute_reply": "2024-04-17T07:29:25.260843Z" } }, "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": "emerging-replacement", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.262460Z", "iopub.status.busy": "2024-04-17T07:29:25.262336Z", "iopub.status.idle": "2024-04-17T07:29:25.403460Z", "shell.execute_reply": "2024-04-17T07:29:25.403173Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
.......................................
229230volkswagenpassat2.020084auto(s6)f1928pmidsize
230231volkswagenpassat2.020084manual(m6)f2129pmidsize
231232volkswagenpassat2.819996auto(l5)f1626pmidsize
232233volkswagenpassat2.819996manual(m5)f1826pmidsize
233234volkswagenpassat3.620086auto(s6)f1726pmidsize
\n", "

234 rows × 12 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 \n", ".. ... ... ... ... ... ... ... .. ... \n", "229 230 volkswagen passat 2.0 2008 4 auto(s6) f 19 \n", "230 231 volkswagen passat 2.0 2008 4 manual(m6) f 21 \n", "231 232 volkswagen passat 2.8 1999 6 auto(l5) f 16 \n", "232 233 volkswagen passat 2.8 1999 6 manual(m5) f 18 \n", "233 234 volkswagen passat 3.6 2008 6 auto(s6) f 17 \n", "\n", " hwy fl class \n", "0 29 p compact \n", "1 29 p compact \n", "2 31 p compact \n", "3 30 p compact \n", "4 26 p compact \n", ".. ... .. ... \n", "229 28 p midsize \n", "230 29 p midsize \n", "231 26 p midsize \n", "232 26 p midsize \n", "233 26 p midsize \n", "\n", "[234 rows x 12 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg" ] }, { "cell_type": "code", "execution_count": 3, "id": "classical-employer", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.404694Z", "iopub.status.busy": "2024-04-17T07:29:25.404572Z", "iopub.status.idle": "2024-04-17T07:29:25.437758Z", "shell.execute_reply": "2024-04-17T07:29:25.437541Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg) + geom_point(aes('displ', 'hwy', color = 'drv'))\n", "p" ] }, { "cell_type": "code", "execution_count": 4, "id": "secure-order", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.439074Z", "iopub.status.busy": "2024-04-17T07:29:25.438856Z", "iopub.status.idle": "2024-04-17T07:29:25.443091Z", "shell.execute_reply": "2024-04-17T07:29:25.442896Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + ggtitle(\"The plot title using 'ggtitle()'\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "opened-edmonton", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.444236Z", "iopub.status.busy": "2024-04-17T07:29:25.444102Z", "iopub.status.idle": "2024-04-17T07:29:25.447983Z", "shell.execute_reply": "2024-04-17T07:29:25.447790Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + labs(title = \"The plot title using 'labs()'\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "marked-world", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.449103Z", "iopub.status.busy": "2024-04-17T07:29:25.448977Z", "iopub.status.idle": "2024-04-17T07:29:25.452959Z", "shell.execute_reply": "2024-04-17T07:29:25.452770Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add subtitle using ggtitle()\n", "\n", "p + ggtitle(\"The plot title\", subtitle = \"The plot subtitle\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "stuffed-reflection", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.454098Z", "iopub.status.busy": "2024-04-17T07:29:25.453972Z", "iopub.status.idle": "2024-04-17T07:29:25.457682Z", "shell.execute_reply": "2024-04-17T07:29:25.457494Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add subtitle using labs()\n", "\n", "p + labs(title = \"The plot title\", subtitle = \"The plot subtitle\")\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "designed-cycle", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.458692Z", "iopub.status.busy": "2024-04-17T07:29:25.458572Z", "iopub.status.idle": "2024-04-17T07:29:25.462341Z", "shell.execute_reply": "2024-04-17T07:29:25.462157Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add caption\n", "\n", "p2 = p + labs(title = \"The plot title\", subtitle = \"The plot subtitle\", caption = \"The plot caption\")\n", "p2" ] }, { "cell_type": "code", "execution_count": 9, "id": "injured-multimedia", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.463365Z", "iopub.status.busy": "2024-04-17T07:29:25.463242Z", "iopub.status.idle": "2024-04-17T07:29:25.467044Z", "shell.execute_reply": "2024-04-17T07:29:25.466854Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add color for title\n", "\n", "# 'title' applies to plot's title, subtitle, caption\n", "p2 + theme(title=element_text(color='blue'))" ] }, { "cell_type": "code", "execution_count": 10, "id": "suitable-grammar", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.468031Z", "iopub.status.busy": "2024-04-17T07:29:25.467887Z", "iopub.status.idle": "2024-04-17T07:29:25.471844Z", "shell.execute_reply": "2024-04-17T07:29:25.471640Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 'plot_title' will also apply to the subtitle\n", "p2 + theme(plot_title=element_text(color='blue'))" ] }, { "cell_type": "code", "execution_count": 11, "id": "radical-terrorism", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.472813Z", "iopub.status.busy": "2024-04-17T07:29:25.472688Z", "iopub.status.idle": "2024-04-17T07:29:25.476817Z", "shell.execute_reply": "2024-04-17T07:29:25.476630Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set own colors\n", "p2 + theme(\n", " plot_title=element_text(color='blue'), \n", " plot_subtitle=element_text(color='red'),\n", " plot_caption=element_text(color='dark_green'))" ] }, { "cell_type": "code", "execution_count": 12, "id": "continued-defeat", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.477781Z", "iopub.status.busy": "2024-04-17T07:29:25.477662Z", "iopub.status.idle": "2024-04-17T07:29:25.481874Z", "shell.execute_reply": "2024-04-17T07:29:25.481687Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multiple lines - using `\\n`\n", "\n", "p + labs(\n", " title = \"The plot title:\\nFuel efficiency for most popular models of car\", \n", " subtitle = \"The plot subtitle:\\nPoints are colored by the type of drive train\",\n", " caption = \"The plot caption:\\nmpg dataset\"\n", " ) + theme(plot_subtitle=element_text(color='gray'), plot_caption=element_text(color='light_gray'))\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "nominated-notebook", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.482845Z", "iopub.status.busy": "2024-04-17T07:29:25.482725Z", "iopub.status.idle": "2024-04-17T07:29:25.487214Z", "shell.execute_reply": "2024-04-17T07:29:25.487024Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Legend title\n", "\n", "p1 = ggplot(mpg) + geom_point(aes('displ', 'hwy', color = 'cty', shape='drv'), size=4) \n", "p1 + labs(color='City mileage', shape='Drive type')" ] }, { "cell_type": "code", "execution_count": 14, "id": "turkish-champagne", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.488190Z", "iopub.status.busy": "2024-04-17T07:29:25.488068Z", "iopub.status.idle": "2024-04-17T07:29:25.492205Z", "shell.execute_reply": "2024-04-17T07:29:25.492023Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change legend position\n", "\n", "p1 + labs(color='City mileage', shape='Drive type') + theme(legend_position='bottom')" ] }, { "cell_type": "code", "execution_count": 15, "id": "affiliated-tiffany", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.493172Z", "iopub.status.busy": "2024-04-17T07:29:25.493047Z", "iopub.status.idle": "2024-04-17T07:29:25.497230Z", "shell.execute_reply": "2024-04-17T07:29:25.497036Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use multiple lines in legend titles\n", "\n", "p1 + labs(color='City mileage\\n(mpg)', \n", " shape='Drive type\\n(front/4/rear wheel)') " ] }, { "cell_type": "code", "execution_count": 16, "id": "portuguese-jonathan", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.498195Z", "iopub.status.busy": "2024-04-17T07:29:25.498070Z", "iopub.status.idle": "2024-04-17T07:29:25.502031Z", "shell.execute_reply": "2024-04-17T07:29:25.501841Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + theme(legend_position='bottom') \\\n", " + labs(color='City mileage\\n(mpg)', \n", " shape='Drive type\\n(front/4/rear wheel)') " ] }, { "cell_type": "code", "execution_count": 17, "id": "interpreted-talent", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:25.502976Z", "iopub.status.busy": "2024-04-17T07:29:25.502854Z", "iopub.status.idle": "2024-04-17T07:29:25.506954Z", "shell.execute_reply": "2024-04-17T07:29:25.506766Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + theme(legend_position='bottom') \\\n", " + labs(color='City mileage\\n(mpg)', \n", " shape='Drive type\\n(front/4/rear wheel)') \\\n", " + scale_shape(guide=guide_legend(nrow=3))" ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }