{ "cells": [ { "cell_type": "markdown", "id": "fd96622a", "metadata": {}, "source": [ "# Templates and colors in the Plotly library" ] }, { "cell_type": "code", "execution_count": null, "id": "87ea7fae", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import plotly.graph_objs as go\n", "import plotly.express as px\n", "import plotly.io as pio" ] }, { "cell_type": "code", "execution_count": null, "id": "1d7655d4", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('Gapminder-data.csv', sep=',')\n", "df = df[df['Year'] == 2020]\n", "df.head()" ] }, { "cell_type": "markdown", "id": "9b30ae37", "metadata": {}, "source": [ "## Templates" ] }, { "cell_type": "markdown", "id": "9affbd8e", "metadata": {}, "source": [ "### All available templates" ] }, { "cell_type": "code", "execution_count": null, "id": "2d706ca1", "metadata": { "scrolled": true }, "outputs": [], "source": [ "for template in ['ggplot2', 'seaborn', 'simple_white', 'plotly', 'plotly_white', 'plotly_dark', \n", " 'presentation', 'xgridoff', 'ygridoff', 'gridon', 'none']:\n", " fig_t = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60,\n", " template=template, title=f'{template} template')\n", " fig_t.show()" ] }, { "cell_type": "markdown", "id": "e2c2aa0e", "metadata": {}, "source": [ "### Setting the default template" ] }, { "cell_type": "code", "execution_count": null, "id": "fff00238", "metadata": {}, "outputs": [], "source": [ "pio.templates.default = 'seaborn'" ] }, { "cell_type": "code", "execution_count": null, "id": "f88c3962", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60)\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "949aa169", "metadata": {}, "source": [ "### Creating a custom template" ] }, { "cell_type": "code", "execution_count": null, "id": "83d5746d", "metadata": {}, "outputs": [], "source": [ "draft_template = go.layout.Template(\n", " layout=dict(annotations=[\n", " dict(\n", " name='draft watermark',\n", " text='DRAFT',\n", " opacity=0.2,\n", " font=dict(color='black', size=100),\n", " xref='paper',\n", " yref='paper',\n", " x=0.5,\n", " y=0.5,\n", " )\n", " ])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "9b91f019", "metadata": {}, "outputs": [], "source": [ "fig_t = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60,\n", " template=draft_template)\n", "fig_t.show()" ] }, { "cell_type": "markdown", "id": "abdaf63c", "metadata": {}, "source": [ "### Registering a template" ] }, { "cell_type": "code", "execution_count": null, "id": "509c0c73", "metadata": {}, "outputs": [], "source": [ "pio.templates['draft'] = draft_template\n", "pio.templates.default = 'draft'" ] }, { "cell_type": "code", "execution_count": null, "id": "a1b76970", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60)\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "3629627e", "metadata": {}, "source": [ "### Combining templates" ] }, { "cell_type": "code", "execution_count": null, "id": "704c58d8", "metadata": {}, "outputs": [], "source": [ "pio.templates.default = 'draft+ggplot2'" ] }, { "cell_type": "code", "execution_count": null, "id": "cd78d264", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60)\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "dd090678", "metadata": {}, "source": [ "### Template settings" ] }, { "cell_type": "code", "execution_count": null, "id": "0a1e4f17", "metadata": { "scrolled": true }, "outputs": [], "source": [ "pio.templates['plotly'].layout" ] }, { "cell_type": "code", "execution_count": null, "id": "1e6238f9", "metadata": { "scrolled": true }, "outputs": [], "source": [ "pio.templates['plotly'].data" ] }, { "cell_type": "markdown", "id": "d495a53e", "metadata": {}, "source": [ "### Template data setting" ] }, { "cell_type": "code", "execution_count": null, "id": "15090bea", "metadata": {}, "outputs": [], "source": [ "symbol_template = go.layout.Template(\n", " data=dict(scatter=[\n", " go.Scatter(marker=dict(symbol='diamond', size=10)),\n", " ])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "4aa6bf23", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60,\n", " template=symbol_template\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "f77b8cc4", "metadata": {}, "outputs": [], "source": [ "pio.templates.default = 'plotly_white'" ] }, { "cell_type": "markdown", "id": "50ce9f2f", "metadata": {}, "source": [ "## Colors" ] }, { "cell_type": "markdown", "id": "65d83039", "metadata": {}, "source": [ "### Ordinal color maps (continuous color scales)" ] }, { "cell_type": "markdown", "id": "0aeeb3a1", "metadata": {}, "source": [ "**Be careful, they are not all perceptually linear!**" ] }, { "cell_type": "code", "execution_count": null, "id": "55a0db4f", "metadata": {}, "outputs": [], "source": [ "fig = px.colors.sequential.swatches_continuous()\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "4d90653b", "metadata": {}, "outputs": [], "source": [ "fig = px.colors.diverging.swatches_continuous()\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "5bfb9746", "metadata": {}, "outputs": [], "source": [ "fig = px.colors.cyclical.swatches_cyclical()\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "dbaeb7f5", "metadata": {}, "source": [ "### Categorical color maps (discrete color sequences)" ] }, { "cell_type": "code", "execution_count": null, "id": "d14e2134", "metadata": {}, "outputs": [], "source": [ "fig = px.colors.qualitative.swatches()\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "abf83680", "metadata": {}, "source": [ "### Using color maps in Plotly" ] }, { "cell_type": "code", "execution_count": null, "id": "70936970", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Region',\n", " log_x=True, \n", " size_max=60,\n", " color_discrete_sequence=px.colors.qualitative.Bold\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "92cbb750", "metadata": {}, "outputs": [], "source": [ "fig = px.scatter(\n", " df,\n", " x='Income', \n", " y='Life expectancy', \n", " size='Population', \n", " color='Child mortality',\n", " log_x=True, \n", " size_max=60,\n", " color_continuous_scale=px.colors.sequential.Viridis\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "cb289097", "metadata": {}, "outputs": [], "source": [] } ], "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }