Last active
October 26, 2023 19:06
-
-
Save vaclavdekanovsky/bd0981c126ae777492be435a5dce38c4 to your computer and use it in GitHub Desktop.
Full code for achieving highlighted Plotly Express line charts on the background of grey lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Highlighted line chart" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import plotly.express as px\n", | |
| "import pandas as pd\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# then load the preprocessed pickles\n", | |
| "long_df = pd.read_pickle(\"../Preprocess/long.plk\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# annotation at the beginning of the line \n", | |
| "# use the \"xanchor\": \"right\" so that the labels stick to the right side of the plot area\n", | |
| "# since you use \"xref\": \"paper\", the annotations are stick to canvas and not the plot area, even if you set y in absolute numbers\n", | |
| "turkey_annotation = \\\n", | |
| "[{\"xref\": \"paper\", \"x\":0, \"y\":7_000_000,\n", | |
| " \"xanchor\":'right', \"yanchor\":\"middle\",\n", | |
| " \"text\":'7M',\n", | |
| " \"font\":dict(family='Arial', size=15, color=\"red\"),\n", | |
| " \"showarrow\":False},\n", | |
| " # end of the line legend\n", | |
| " # use the \"xanchor\": \"left\" so that the labels stick to the right side of the plot area\n", | |
| " {\"xref\":\"paper\", \"x\":1, \"y\":45_000_000,\n", | |
| " \"xanchor\":\"left\", \"yanchor\":\"middle\",\n", | |
| " \"text\":'Turkey (45M)',\n", | |
| " \"font\":dict(family='Arial', size=15, color=\"red\"),\n", | |
| " \"showarrow\":False},]\n", | |
| "\n", | |
| "japan_annotation = [{\"xref\": \"paper\", \"x\":0, \"y\":3_300_000,\n", | |
| " \"xanchor\":'right', \"yanchor\":'middle',\n", | |
| " \"text\":'3.3M',\n", | |
| " \"font\":dict(family='Arial',\n", | |
| " size=15,\n", | |
| " color=\"blue\"),\n", | |
| " \"showarrow\":False}, \n", | |
| " {\"xref\":\"paper\", \"x\":1, \"y\":31_000_000,\n", | |
| " \"xanchor\":'left', \"yanchor\":'middle',\n", | |
| " \"text\":'Japan (31M)',\n", | |
| " \"font\":dict(family='Arial',\n", | |
| " size=15,\n", | |
| " color=\"blue\"),\n", | |
| " \"showarrow\":False}]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# add an arrow pointing to interesting point\n", | |
| "tourism_boom_in_japan_annotation = [{\"x\":2011, \"y\":6220000, \"ay\": -40, \n", | |
| " \"text\": \"<b>Tourism Boom<br> in Japan 2011</b>\",\n", | |
| " \"arrowhead\": 3, \"showarrow\":True,\n", | |
| " \"font\": {\"size\": 15}, \"bgcolor\":None, \"textangle\": 0}]\n", | |
| "\n", | |
| "# add the description in the middle of the plot\n", | |
| "area_annotation = [{\"x\":2007, \"y\":40000000, \n", | |
| " \"text\": \"<b>Number of tourist is growing</b>\",\n", | |
| " \"textangle\": -25,\n", | |
| " \"showarrow\":False,\n", | |
| " \"bgcolor\":\"lightblue\",\n", | |
| " \"font\": {\"size\": 15}}]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Country Name</th>\n", | |
| " <th>Country Code</th>\n", | |
| " <th>Region</th>\n", | |
| " <th>years</th>\n", | |
| " <th>visitors</th>\n", | |
| " <th>receipts</th>\n", | |
| " <th>order</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>4945</th>\n", | |
| " <td>Aruba</td>\n", | |
| " <td>ABW</td>\n", | |
| " <td>Latin America & Caribbean</td>\n", | |
| " <td>2018</td>\n", | |
| " <td>1082000.0</td>\n", | |
| " <td>2.024000e+09</td>\n", | |
| " <td>3.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4946</th>\n", | |
| " <td>Afghanistan</td>\n", | |
| " <td>AFG</td>\n", | |
| " <td>South Asia</td>\n", | |
| " <td>2018</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>5.000000e+07</td>\n", | |
| " <td>3.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4947</th>\n", | |
| " <td>Angola</td>\n", | |
| " <td>AGO</td>\n", | |
| " <td>Sub-Saharan Africa</td>\n", | |
| " <td>2018</td>\n", | |
| " <td>218000.0</td>\n", | |
| " <td>5.570000e+08</td>\n", | |
| " <td>3.0</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Country Name Country Code Region years visitors \\\n", | |
| "4945 Aruba ABW Latin America & Caribbean 2018 1082000.0 \n", | |
| "4946 Afghanistan AFG South Asia 2018 0.0 \n", | |
| "4947 Angola AGO Sub-Saharan Africa 2018 218000.0 \n", | |
| "\n", | |
| " receipts order \n", | |
| "4945 2.024000e+09 3.0 \n", | |
| "4946 5.000000e+07 3.0 \n", | |
| "4947 5.570000e+08 3.0 " | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# sort the dataframe\n", | |
| "sorted_df = long_df.copy()\n", | |
| "\n", | |
| "# map the value order\n", | |
| "sorted_df[\"order\"] = sorted_df[\"Country Name\"].map({\"Japan\": 1, \"Turkey\": 2}).fillna(3)\n", | |
| "\n", | |
| "# sort by this order\n", | |
| "sorted_df.sort_values(by=[\"order\",\"years\"], ascending=False, inplace=True)\n", | |
| "sorted_df.head(3)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "fig = px.line(sorted_df,\n", | |
| " x=\"years\",\n", | |
| " y=\"visitors\", \n", | |
| " color=\"Country Name\", \n", | |
| " line_group=\"Country Name\")\n", | |
| "\n", | |
| "# set color of all traces to lightgrey\n", | |
| "fig.update_traces({\"line\":{\"color\":\"lightgrey\"}})\n", | |
| "\n", | |
| "# color Turkish line to blue\n", | |
| "fig.update_traces(patch={\"line\":{\"color\":\"blue\", \"width\":5}}, \n", | |
| " selector={\"legendgroup\":\"Turkey\"})\n", | |
| "\n", | |
| "# color Japanese line to red\n", | |
| "fig.update_traces(patch={\"line\":{\"color\":\"red\", \"width\":5}}, \n", | |
| " selector={\"legendgroup\":\"Japan\"})\n", | |
| "\n", | |
| "fig.update_layout(title=\"Tourism Growth in Turkey and Japan\",\n", | |
| " # remove the legent\n", | |
| " showlegend=False,\n", | |
| " \n", | |
| " # make y-axis invisible\n", | |
| " yaxis={\"visible\":False},\n", | |
| " \n", | |
| " # create the annoations\n", | |
| " # point annotattion\n", | |
| " annotations=turkey_annotation+japan_annotation+tourism_boom_in_japan_annotation+area_annotation,\n", | |
| " \n", | |
| " # adjust the margin, so that annotations fit into the canvas\n", | |
| " margin={\"l\":50, \"r\": 100})\n", | |
| "fig.show()" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "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.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment