{ "cells": [ { "cell_type": "markdown", "id": "3dc9150a", "metadata": {}, "source": [ "# Joint plot" ] }, { "cell_type": "markdown", "id": "9e289998", "metadata": {}, "source": [ "## Table of Contents\n", "\n", "1. [Default Presentation of Joint Plot](#default)\n", "\n", "2. [Change Geom Types](#change-geom)\n", "\n", "3. [Geometries Customization](#geom-customization)\n", "\n", "4. [Marginal Layers Customization](#marginal-customization)\n", "\n", "5. [Grouping](#grouping)\n", "\n", "6. [Additional Layer](#additional_layer)" ] }, { "cell_type": "code", "execution_count": 1, "id": "2cb4b712", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot.bistro import *\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "411b248e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "df4a9ae6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 5)\n" ] }, { "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", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 setosa\n", "1 4.9 3.0 1.4 0.2 setosa\n", "2 4.7 3.2 1.3 0.2 setosa\n", "3 4.6 3.1 1.5 0.2 setosa\n", "4 5.0 3.6 1.4 0.2 setosa" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "markdown", "id": "75f00ac0", "metadata": {}, "source": [ "\n", "\n", "## 1. Default Presentation of Joint Plot\n", "\n", "In the simplest case, assign `x` and `y` to create a scatterplot (using `geom_point()`) with marginal histograms (using `geom_histogram()`)." ] }, { "cell_type": "code", "execution_count": 4, "id": "76bddb5b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df, \"petal_length\", \"petal_width\")" ] }, { "cell_type": "markdown", "id": "5da4eb92", "metadata": {}, "source": [ "\n", "\n", "## 2. Change Geom Types\n", "\n", "Besides the points there are another two types of geoms: `tile` and `density2d(f)`." ] }, { "cell_type": "code", "execution_count": 5, "id": "20fec1dc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df, \"petal_length\", \"petal_width\", geom='tile')" ] }, { "cell_type": "code", "execution_count": 6, "id": "61ea146c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df.dropna(), \"petal_length\", \"petal_width\", color_by=\"species\", geom='density2d')" ] }, { "cell_type": "markdown", "id": "2e6a14f3", "metadata": {}, "source": [ "\n", "\n", "## 3. Change Geom Parameters\n", "\n", "Use additional parameters for better customization: `color`, `size`, `alpha`, etc." ] }, { "cell_type": "code", "execution_count": 7, "id": "6c9ef64c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df, \"petal_length\", \"petal_width\", color=\"#756bb1\", size=8, alpha=.5, se=False)" ] }, { "cell_type": "markdown", "id": "953dfc07", "metadata": {}, "source": [ "\n", "\n", "## 4. Marginal Layers Customization\n", "\n", "`marginal` parameter is a shortcut for the `ggmarginal()` layer." ] }, { "cell_type": "code", "execution_count": 8, "id": "ffa9a0db", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df, \"petal_length\", \"petal_width\", color=\"black\", marginal=\"box:lb:.03,hist:t:.4,hist:r\") + \\\n", " ggmarginal(\"tr\", layer=geom_area(stat='density', color=\"magenta\", fill=\"magenta\", alpha=.1)) + \\\n", " theme(axis_line_x='blank', axis_line_y='blank')" ] }, { "cell_type": "markdown", "id": "b57000df", "metadata": {}, "source": [ "\n", "\n", "## 5. Grouping\n", "\n", "The `color_by` parameter sets the mapping to the fill and color aesthetics." ] }, { "cell_type": "code", "execution_count": 9, "id": "922e790f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df, \"petal_length\", \"petal_width\", color_by=\"species\", marginal=\"hist:tr\")" ] }, { "cell_type": "markdown", "id": "20225054", "metadata": {}, "source": [ "\n", "\n", "## 6. Additional Layer\n", "\n", "Add any other layer that supports `x` and `y` aesthetics (e.g. points layer with the `geom_point()` function)." ] }, { "cell_type": "code", "execution_count": 10, "id": "20dbcf35", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_plot(df, \"petal_length\", \"petal_width\", geom='density2df', \\\n", " color=\"#993404\", alpha=1/3, reg_line=False) + \\\n", " geom_point(size=5, shape=21, color=\"#993404\", fill=\"#ffffd4\") + \\\n", " scale_fill_gradient(low=\"#d95f0e\", high=\"#fff7bc\")" ] } ], "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.15" } }, "nbformat": 4, "nbformat_minor": 5 }