{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "first-mailing", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:25.107936Z", "iopub.status.busy": "2024-04-26T12:07:25.107936Z", "iopub.status.idle": "2024-04-26T12:07:26.049955Z", "shell.execute_reply": "2024-04-26T12:07:26.049955Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "21712102", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.049955Z", "iopub.status.busy": "2024-04-26T12:07:26.049955Z", "iopub.status.idle": "2024-04-26T12:07:26.065603Z", "shell.execute_reply": "2024-04-26T12:07:26.065603Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "respiratory-pressure", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.065603Z", "iopub.status.busy": "2024-04-26T12:07:26.065603Z", "iopub.status.idle": "2024-04-26T12:07:26.223668Z", "shell.execute_reply": "2024-04-26T12:07:26.223668Z" } }, "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": 3, "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": 4, "id": "human-invasion", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.223668Z", "iopub.status.busy": "2024-04-26T12:07:26.223668Z", "iopub.status.idle": "2024-04-26T12:07:26.333779Z", "shell.execute_reply": "2024-04-26T12:07:26.333779Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = (\n", " ggplot(mpg, aes('displ', 'hwy')) + \n", " geom_point() + \n", " labs(\n", " title = \"Fuel efficiency\\nfor most popular models of car\",\n", " subtitle = \"Period 1999-2008\",\n", " caption = \"MPG Dataset\",\n", " x=\"Engine displacement\\n(litres)\", \n", " y = \"Highway\\nmiles per gallon\"\n", " ) + \n", " theme_classic() + \n", " theme(plot_background=element_rect(size=1))\n", ")\n", "p" ] }, { "cell_type": "code", "execution_count": 5, "id": "legislative-sleeve", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.333779Z", "iopub.status.busy": "2024-04-26T12:07:26.333779Z", "iopub.status.idle": "2024-04-26T12:07:26.349235Z", "shell.execute_reply": "2024-04-26T12:07:26.349235Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change margins around plot title, subtitle, caption and axis titles\n", "\n", "p + theme(plot_title=element_text(margin=[15, None, None]), \n", " plot_subtitle=element_text(margin=[None, None, 10]),\n", " plot_caption=element_text(margin=[None, None, 15]),\n", " axis_title_x=element_text(margin=[10, None, None]), \n", " axis_title_y=element_text(margin=[None, 10, None, 15]))" ] }, { "cell_type": "code", "execution_count": 6, "id": "coordinate-situation", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.349235Z", "iopub.status.busy": "2024-04-26T12:07:26.349235Z", "iopub.status.idle": "2024-04-26T12:07:26.364970Z", "shell.execute_reply": "2024-04-26T12:07:26.364970Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change margins around axis tick labels\n", "\n", "p + theme(axis_text_x=element_text(margin=[10, None, 15]), \n", " axis_text_y=element_text(margin=[None, 10, None, 15]))" ] }, { "cell_type": "code", "execution_count": 7, "id": "terminal-picnic", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.364970Z", "iopub.status.busy": "2024-04-26T12:07:26.364970Z", "iopub.status.idle": "2024-04-26T12:07:26.380575Z", "shell.execute_reply": "2024-04-26T12:07:26.380575Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# axis labels at different levels\n", "\n", "p2 = ggplot(mpg, aes('class', 'hwy')) + geom_boxplot()\n", "p2" ] }, { "cell_type": "code", "execution_count": 8, "id": "registered-inspector", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.380575Z", "iopub.status.busy": "2024-04-26T12:07:26.380575Z", "iopub.status.idle": "2024-04-26T12:07:26.396238Z", "shell.execute_reply": "2024-04-26T12:07:26.396238Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + theme(axis_text_x=element_text(margin=[10, None]))" ] }, { "cell_type": "code", "execution_count": 9, "id": "worth-survey", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.397911Z", "iopub.status.busy": "2024-04-26T12:07:26.397911Z", "iopub.status.idle": "2024-04-26T12:07:26.412097Z", "shell.execute_reply": "2024-04-26T12:07:26.412097Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# with rotation\n", "\n", "p2 + ggsize(550, 400)" ] }, { "cell_type": "code", "execution_count": 10, "id": "corrected-actress", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:07:26.412097Z", "iopub.status.busy": "2024-04-26T12:07:26.412097Z", "iopub.status.idle": "2024-04-26T12:07:26.428023Z", "shell.execute_reply": "2024-04-26T12:07:26.428023Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2 + ggsize(550, 400)+ theme(axis_text_x=element_text(margin=[10, None, None]))" ] } ], "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 }