{ "cells": [ { "cell_type": "markdown", "id": "e8d8eaf2", "metadata": {}, "source": [ "# A post-punk chart remake\n", "\n", "Original: https://blog.datawrapper.de/weekly-ridgeline-plot/" ] }, { "cell_type": "code", "execution_count": 1, "id": "29a1a1d1", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:39:17.071823Z", "iopub.status.busy": "2024-04-17T07:39:17.071735Z", "iopub.status.idle": "2024-04-17T07:39:17.394153Z", "shell.execute_reply": "2024-04-17T07:39:17.393699Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "081d0161", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:39:17.395752Z", "iopub.status.busy": "2024-04-17T07:39:17.395602Z", "iopub.status.idle": "2024-04-17T07:39:17.397904Z", "shell.execute_reply": "2024-04-17T07:39:17.397720Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "037078d1", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:39:17.398818Z", "iopub.status.busy": "2024-04-17T07:39:17.398747Z", "iopub.status.idle": "2024-04-17T07:39:17.400438Z", "shell.execute_reply": "2024-04-17T07:39:17.400258Z" } }, "outputs": [], "source": [ "def dataset_array_to_dataframe(dataset_array):\n", " df = pd.DataFrame.from_records([\n", " (j, i, a)\n", " for i, r in enumerate(dataset_array)\n", " for j, a in enumerate(r)\n", " ], columns=[\"x\", \"y\", \"h\"])\n", " df.h = df.h + abs(df.h.min())\n", " return df" ] }, { "cell_type": "code", "execution_count": 4, "id": "57c086aa", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:39:17.401223Z", "iopub.status.busy": "2024-04-17T07:39:17.401145Z", "iopub.status.idle": "2024-04-17T07:39:17.856803Z", "shell.execute_reply": "2024-04-17T07:39:17.856509Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(24000, 3)\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", "
xyh
0005.24
1105.14
2204.96
3305.05
4405.46
\n", "
" ], "text/plain": [ " x y h\n", "0 0 0 5.24\n", "1 1 0 5.14\n", "2 2 0 4.96\n", "3 3 0 5.05\n", "4 4 0 5.46" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_data_array = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/pulsar.csv\", header=None).to_numpy()\n", "df = dataset_array_to_dataframe(raw_data_array)\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "195a3d40", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:39:17.857991Z", "iopub.status.busy": "2024-04-17T07:39:17.857894Z", "iopub.status.idle": "2024-04-17T07:39:17.990615Z", "shell.execute_reply": "2024-04-17T07:39:17.990369Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df) + \\\n", " geom_area_ridges(aes(\"x\", \"y\", height=\"h\"), \\\n", " stat='identity', scale=.25, \\\n", " color=\"white\", fill=\"black\",\n", " sampling=sampling_pick(df.shape[0])) + \\\n", " scale_y_continuous(trans='reverse') + \\\n", " ggsize(600, 600) + \\\n", " theme_minimal() + theme(axis='blank', panel_grid='blank') + \\\n", " flavor_high_contrast_dark()" ] } ], "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 }