{ "cells": [ { "cell_type": "markdown", "id": "de1c1ba0", "metadata": {}, "source": [ "It is always desired to support blog posts with good visuals and the interactive plots are very good for exploration and a better understanding of the results and underlying data. Including interactive plots involves the usage of javascript for the given visualization library and makes embedding interactive figures not that straightforward. This post provides an exploration of methods used to embed interactive plot in jupyter notebook that is later transformed into an HTML webpage. The article is divided into three parts dedicated to major python plotting libraries: [plotly](https://plotly.com/python/), [bokeh](https://bokeh.org/) , and [altair](https://altair-viz.github.io/).\n", "\n", "\n", "\n", "- [Plotly](#plotly)\n", " - [Create baseline plot data](#create-baseline-plot-data)\n", " - [Display](#display)\n", " - [Method 1: template for js snippet \\(Figure is displayed twice\\)](#method-1-template-for-js-snippet-figure-is-displayed-twice)\n", " - [Method 2: Dump plotly figure to HTML string & pass to `display.HTML()` \\(no display in notebook\\)](#method-2-dump-plotly-figure-to-html-string--pass-to-displayhtml-no-display-in-notebook)\n", " - [Method 3. with HTML with plotly figure and display in IFrame](#method-3-with-html-with-plotly-figure-and-display-in-iframe)\n", "- [Bokeh](#bokeh)\n", "- [Altair](#altair)\n", "\n", "" ] }, { "cell_type": "markdown", "id": "79d34410", "metadata": { "hideCode": false, "hidePrompt": false }, "source": [ "\n", "# Plotly" ] }, { "cell_type": "markdown", "id": "95b9307c", "metadata": { "hideCode": false, "hidePrompt": false }, "source": [ "\n", "## Create baseline plot data" ] }, { "cell_type": "code", "execution_count": 1, "id": "2772fb44", "metadata": { "hideCode": false, "hidePrompt": false }, "outputs": [], "source": [ "import plotly\n", "from plotly import express as px\n", "\n", "gapminder = px.data.gapminder()\n", "\n", "fig = px.scatter(\n", " gapminder.query(\"year==2007\"),\n", " x=\"gdpPercap\",\n", " y=\"lifeExp\",\n", " size=\"pop\",\n", " color=\"continent\",\n", " hover_name=\"country\",\n", " log_x=True,\n", " size_max=60,\n", " height=480,\n", " width=600,\n", ")" ] }, { "cell_type": "markdown", "id": "4594c01c", "metadata": { "hideCode": false, "hidePrompt": false }, "source": [ "\n", "## Display" ] }, { "cell_type": "markdown", "id": "232b5cd0", "metadata": { "hideCode": false, "hidePrompt": false }, "source": [ "\n", "### Method 1: template for js snippet (Figure is displayed twice)\n", "In this solution display(HTML) is rendering proper plot for the blog, and the fig.show() provides preview while working in the notebook." ] }, { "cell_type": "code", "execution_count": 2, "id": "048bad61", "metadata": { "hideCode": false, "hidePrompt": false }, "outputs": [], "source": [ "plot_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)" ] }, { "cell_type": "code", "execution_count": 3, "id": "3ab6dd14", "metadata": { "hideCode": false, "hidePrompt": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "" ], "text/plain": [ "\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"
\\n\"+\n",
" \"\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"
\\n\"+\n \"\n", " | x | \n", "y | \n", "
---|---|---|
0 | \n", "1.625303 | \n", "-3.450590 | \n", "
1 | \n", "0.331879 | \n", "-6.498201 | \n", "