{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import lux" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial, we look at the [Happy Planet Index](http://happyplanetindex.org/) dataset, which contains metrics related to well-being for 140 countries around the world. We demonstrate how you can select visualizations of interest and export them for further analysis. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('https://github.com/lux-org/lux-datasets/blob/master/data/hpi.csv?raw=true')\n", "lux.config.default_display = \"lux\" # Set Lux as default display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that for the convienience of this tutorial, we have set Lux as the default display so we don't have to Toggle from the Pandas table display everytime we print the dataframe." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exporting widget visualizations as static HTML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say that you are interested in sharing the visualizations displayed in Lux with others, you can export the visualizations into a static HTML using the following command: " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.save_as_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then you can open up this file on your browser, or share the HTML file with someone." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Opening" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, the file is saved as `export.html`, you can optionally specify the HTML filename in the input parameter. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.save_as_html('hpi.html')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Selecting visualizations from recommendation widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also click on visualizations of interest and export them into a separate widget for further processing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bookmarked_charts = df.exported\n", "bookmarked_charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the dataframe recommendations, the visualization showing the relationship between `GDPPerCapita` and `Footprint` is very interesting. In particular, there is an outlier with extremely high ecological footprint as well as high GDP per capita. So we click on this visualization and click on the export button." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Click on the GDPPerCapita v.s. Footprint vis and export it first before running this cell\n", "vis = df.exported[0]\n", "vis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting Vis as the Updated Intent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often, we might be interested in other visualizations that is related to a visualization of interest and want to learn more. With the exported Vis, we can update the intent associated with dataframe to be based on the selected Vis to get more recommendations related to this visualization." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.intent = vis\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing Widget State" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access the set of recommendations generated for the dataframes via the properties `recommendation`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "df.recommendation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The resulting output is a dictionary, keyed by the name of the recommendation category." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "df.recommendation[\"Enhance\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also access the vis represented by the current intent via the property `current_vis`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.current_vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exporting Visualizations as Code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's revist our earlier recommendations by clearing the specified intent." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.clear_intent()\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looking at the Occurrence tab, we are interested in the bar chart distribution of country `SubRegion`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis = df.recommendation[\"Occurrence\"][0]\n", "vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To allow further edits of visualizations, visualizations can be exported to code in [Matplotlib](https://matplotlib.org/), [Altair](https://altair-viz.github.io/), or as [Vega-Lite](https://vega.github.io/vega-lite/) specification via the `to_code` command:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (vis.to_code(\"matplotlib\"))\n", "print (vis.to_code(\"altair\"))\n", "print (vis.to_code(\"vegalite\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exporting Visualizations to Matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also export the visualization as code in [Matplotlib](https://matplotlib.org/)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (vis.to_matplotlib())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be copy-and-pasted back into a new notebook cell for further editing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "plt.rcParams.update(\n", " {\n", " \"axes.titlesize\": 20,\n", " \"axes.titleweight\": \"bold\",\n", " \"axes.labelweight\": \"bold\",\n", " \"axes.labelsize\": 16,\n", " \"legend.fontsize\": 14,\n", " \"legend.title_fontsize\": 15,\n", " \"xtick.labelsize\": 13,\n", " \"ytick.labelsize\": 13,\n", " }\n", " )\n", "import numpy as np\n", "from math import nan\n", "visData = pd.DataFrame({'SubRegion': {0: 'Americas', 1: 'Asia Pacific', 2: 'Europe', 3: 'Middle East and North Africa', 4: 'Post-communist', 5: 'Sub Saharan Africa'}, 'Record': {0: 25, 1: 21, 2: 20, 3: 14, 4: 26, 5: 34}})\n", "fig, ax = plt.subplots()\n", "bars = visData['SubRegion']\n", "measurements = visData['Record']\n", "ax.barh(bars, measurements, align='center')\n", "ax.set_xlabel('Record')\n", "ax.set_ylabel('SubRegion')\n", "\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exporting Visualizations to Altair" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "visData = pd.DataFrame({'SubRegion': {0: 'Americas', 1: 'Asia Pacific', 2: 'Europe', 3: 'Middle East and North Africa', 4: 'Post-communist', 5: 'Sub Saharan Africa'}, 'Record': {0: 25, 1: 21, 2: 20, 3: 14, 4: 26, 5: 34}})\n", "\n", "chart = alt.Chart(visData).mark_bar().encode(\n", " y = alt.Y('SubRegion', type= 'nominal', axis=alt.Axis(labelOverlap=True), sort ='-x'),\n", " x = alt.X('Record', type= 'quantitative', title='Count of Record'),\n", ")\n", "chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding')) # Setting tooltip as non-null\n", "chart = chart.configure_title(fontWeight=500,fontSize=13,font='Helvetica Neue')\n", "chart = chart.configure_axis(titleFontWeight=500,titleFontSize=11,titleFont='Helvetica Neue',\n", "\t\t\tlabelFontWeight=400,labelFontSize=8,labelFont='Helvetica Neue',labelColor='#505050')\n", "chart = chart.configure_legend(titleFontWeight=500,titleFontSize=10,titleFont='Helvetica Neue',\n", "\t\t\tlabelFontWeight=400,labelFontSize=8,labelFont='Helvetica Neue')\n", "chart = chart.properties(width=160,height=150)\n", "chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exporting Visualizations to Vega-Lite" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also export this as Vega-Lite specification and vis/edit the specification on [Vega Editor](https://vega.github.io/editor).\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "print (vis.to_vegalite())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"add" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exporting Standalone Visualizations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say now we are interested in the scatter plot of the `HPIRank` and `HappyPlanetIndex`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis = df.recommendation[\"Correlation\"][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the dataframes associated with points on a scatterplot is large, by default Lux infers the variable name used locally for the data, and uses that as the data in the printed code block." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (vis.to_altair())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we wanted to include the actual data in the returned codeblock, we would use `to_altair(standalone=True)` to create a code snippet that contains all the data that we need embedded in the code itself, which can be run outside the notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (vis.to_altair(standalone=True))" ] } ], "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.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }