{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "aed85edd-713f-43c6-a1ee-6d07b31192fe", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "cdf544b2-0af6-42c4-b10a-629cd5b36aa3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "a6348b82-1288-4509-8910-f54ab4fc6dde", "metadata": {}, "source": [ "# Function `parallel_coordinates_plot()`" ] }, { "cell_type": "code", "execution_count": 3, "id": "7ba8c32f-7dd4-4c4c-8555-232db94027f5", "metadata": {}, "outputs": [], "source": [ "def parallel_coordinates_plot(df: pd.DataFrame, class_column_name, order=None):\n", " plot_df = df.copy()\n", " plot_df.insert(0, 'par_plot_idx', list(range(df.shape[0])))\n", "\n", " var_names = df.columns.tolist()\n", " var_names.remove(class_column_name)\n", "\n", " plot_df = plot_df.melt(\n", " id_vars=[class_column_name, 'par_plot_idx'],\n", " value_vars= var_names\n", " )\n", " \n", " p = ggplot(plot_df, aes(x='variable', y='value', color=class_column_name, group='par_plot_idx')) \\\n", " + geom_line(size=1)\n", "\n", " if order is not None:\n", " p += scale_x_discrete(breaks=order)\n", " \n", " return p" ] }, { "cell_type": "markdown", "id": "74a882f8-c3e8-448f-8ed1-c326e7458517", "metadata": {}, "source": [ "# Demo" ] }, { "cell_type": "code", "execution_count": 4, "id": "7311ac4d-5018-4f1e-a84a-7113f025eddd", "metadata": {}, "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", "
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": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv')\n", "iris.head()" ] }, { "cell_type": "markdown", "id": "4f45b226-af70-4a77-8eef-e4af9c6be2f5", "metadata": {}, "source": [ "### Default plot" ] }, { "cell_type": "code", "execution_count": 5, "id": "06ce4669-63f6-454a-a830-cfc98995f04a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parallel_coordinates_plot(iris, 'species')" ] }, { "cell_type": "markdown", "id": "92b6f748-2d39-44c8-accd-77e0dd21fb57", "metadata": {}, "source": [ "## Reordered columns with the `order` parameter" ] }, { "cell_type": "code", "execution_count": 6, "id": "18fc9754-6301-4849-87ff-287833b9446d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parallel_coordinates_plot(iris, 'species', order=['petal_width', 'sepal_width', 'petal_length', 'sepal_length'])" ] }, { "cell_type": "markdown", "id": "ccec8039-0920-420f-8640-7034bf93cd55", "metadata": {}, "source": [ "See also: \n", "https://r-graph-gallery.com/parallel-plot.html" ] } ], "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 }