{ "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 how you can select visualizations of interest from the Lux widget and export them for further analysis. We will be working with the [Happy Planet Index](http://happyplanetindex.org/) dataset, which contains metrics related to well-being for 140 countries around the world. " ] }, { "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": [ "For the convienience of this tutorial, we set Lux as the default display to avoid having to Toggle from the Pandas table display everytime." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting one or more visualizations from widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Lux, you can click on visualizations of interest and export them into a separate widget for further processing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Select charts from the widget above and click on the export button to access them here.\n", "df.exported" ] }, { "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. Select 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": [ "# Select the GDP by Footprint visualization and export it to the `gdp_footprint` variable\n", "gdp_footprint = df.exported[0]\n", "gdp_footprint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting the Intent with a Vis input" ] }, { "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 = gdp_footprint\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting Visualizations as Code" ] }, { "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (gdp_footprint.to_altair())" ] }, { "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 altair as alt\n", "\n", "chart = alt.Chart(df).mark_circle().encode(\n", " x=alt.X('Footprint',scale=alt.Scale(domain=(0.6, 15.8)),type='quantitative'),\n", " y=alt.Y('GDPPerCapita',scale=alt.Scale(domain=(244, 105447)),type='quantitative')\n", ")\n", "chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding')) # Setting tooltip as non-null\n", "chart = chart.interactive() # Enable Zooming and Panning\n", "\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", "\n", "chart" ] }, { "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 (gdp_footprint.to_vegalite())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Visualizations can also be exported to code in [Matplotlib](https://matplotlib.org/)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (gdp_footprint.to_matplotlib())\n", "print (gdp_footprint.to_code(language=\"matplotlib\"))" ] }, { "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", "from matplotlib.cm import ScalarMappable\n", "fig, ax = plt.subplots(figsize=(4.5, 4))\n", "x_pts = df['InequalityAdjustedWellbeing']\n", "y_pts = df['AverageWellBeing']\n", "ax.scatter(x_pts, y_pts, alpha=0.5)\n", "ax.set_xlabel('InequalityAdjus...dWellbeing', fontsize='15')\n", "ax.set_ylabel('AverageWellBeing', fontsize='15')\n", "\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }