{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import lux\n", "\n", "url = 'https://github.com/lux-org/lux-datasets/blob/master/data/olympic.csv?raw=true'\n", "df = pd.read_csv(url)\n", "df[\"Year\"] = pd.to_datetime(df[\"Year\"], format='%Y') # change pandas dtype for the column \"Year\" to datetype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the earlier tutorials, we have seen how Lux recommends visualizations automatically to the user. Often, the user might have a particular visualizations in mind that they want to specify. In this case, users can quickly define their own visualizations using Lux and visualize their data on-demand.\n", "\n", "In this tutorial, we will introduce how to create a visualization via the `Vis` object and a collection of visualization via the `VisList` object. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Desired Visualizations On-Demand using `Vis`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `Vis` object represents an individual visualization displayed in Lux, which can either be automatically generated or defined by the user.\n", "\n", "To generate a `Vis`, users should specify their intent and a source dataframe as inputs. The intent is expressed using the same intent specification language described in the last tutorial. \n", "\n", "For example, here we indicate our intent for visualizing the `Weight` attribute on the dataframe `df`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from lux.vis.Vis import Vis\n", "intent = [\"Weight\"]\n", "vis = Vis(intent,df)\n", "vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can very easily replace the Vis's source data without changing the `Vis` definition, which is useful for comparing differences across different datasets with the same schema. \n", "\n", "For example, we might be interested in the same `Weight` distribution, but plotted only on the subset of data with female athletes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis.refresh_source(df[df[\"Sex\"]=='F'])\n", "vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Likewise, we can modify the intent of the query, in this case, to increase the bin size of the histogram and to indicate the filtered source:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_intent = [lux.Clause(\"Weight\",bin_size=50),\"Sex=F\"]\n", "vis.set_intent(new_intent)\n", "vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Vis` objects are powerful programmatic representations of visualizations that can be exported into visualization code (more in the next tutorial) or be composed into a `VisList` collection." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with Collections of Visualization with `VisList`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`VisList` objects represent collections of visualizations in Lux.\n", "\n", "There are two ways to specify lists of visualization in Lux: 1) by specifying intent or 2) by manually composing `Vis` object into a list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Approach #1: Specifying `VisList` using intent syntax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we look at an example of a `VisList` created through a user intent. Here, we create a vis collection of `Weight` with respect to all other attributes, using the wildcard \"?\" symbol." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from lux.vis.VisList import VisList\n", "vc = VisList([\"Weight\",\"?\"],df)\n", "vc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, we can specify desired attributes via a list with respect to `Weight`: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "vc = VisList([\"Weight\",['Sport','Year','Height','HostRegion','SportType']],df)\n", "vc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Approach #2: Specifying `VisList` by constructing `Vis` objects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`VisList` can be manually constructed by individually specifying the content of each `Vis`, then finally putting the entire list into a `VisList` object.\n", "\n", "Here is the equivalent `VisList` example constructed using this approach:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from lux.vis.VisList import VisList\n", "\n", "vcLst = []\n", "for attribute in ['Sport','Year','Height','HostRegion','SportType']: \n", " vis = Vis([lux.Clause(\"Weight\"), lux.Clause(attribute)])\n", " vcLst.append(vis)\n", "vc = VisList(vcLst,df)\n", "\n", "vc" ] } ], "metadata": { "history": [ { "time": "2020-07-22T07:57:15.318Z", "type": "kernelReady" }, { "time": "2020-07-22T08:31:29.656Z", "type": "kernelReady" }, { "code": "# Specifying visualizations", "id": "7beb732dadcc44978ccfc1157a23726f", "order_count": 2, "time": "2020-07-22T08:31:32.615Z", "type": "selectMarkdownCell" }, { "code": "# Specifying visualizations", "id": "7beb732dadcc44978ccfc1157a23726f", "order_count": 2, "time": "2020-07-22T08:31:32.803Z", "type": "selectMarkdownCell" }, { "code": "# Specifying visualizations", "id": "7beb732dadcc44978ccfc1157a23726f", "order_count": 2, "time": "2020-07-22T08:31:33.551Z", "type": "editMarkdownCell" }, { "code": "# Specifying visualizations", "id": "7beb732dadcc44978ccfc1157a23726f", "order_count": 2, "time": "2020-07-22T08:31:33.959Z", "type": "selectMarkdownCell" }, { "code": "Vis objects represents individual visualizations displayed in Lux. Lists of visualizations are stored as VisList objects. visualizations can either be automatically generated in Lux or defined by the user.\n", "id": "a8353b127475432986a519f31b767e58", "order_count": 3, "time": "2020-07-22T08:31:38.691Z", "type": "selectMarkdownCell" }, { "code": "# Specifying Vis Collections", "id": "0c6ad49c981e4cdd8e247c60e303a1dd", "order_count": 9, "time": "2020-07-22T08:31:41.126Z", "type": "selectMarkdownCell" }, { "code": "# Specifying Vis Collections", "id": "0c6ad49c981e4cdd8e247c60e303a1dd", "order_count": 9, "time": "2020-07-22T08:31:41.276Z", "type": "selectMarkdownCell" }, { "code": "# Specifying Vis Collections", "id": "0c6ad49c981e4cdd8e247c60e303a1dd", "order_count": 9, "time": "2020-07-22T08:31:41.957Z", "type": "editMarkdownCell" }, { "code": "# Specifying Vis Collections", "id": "0c6ad49c981e4cdd8e247c60e303a1dd", "order_count": 9, "time": "2020-07-22T08:31:42.495Z", "type": "selectMarkdownCell" }, { "code": "There are two different ways to specify vis collections in Lux: 1) via Query or 2) via manual Vis construction.\n\nFirst, we see an example of a vis collection created in a similiar procedure as visualizations through the Query syntax. Here, we create a vis collection of `Weight` with respect to all other attributes.", "id": "f66167de4c96431fb571baa8f1131a28", "order_count": 10, "time": "2020-07-22T08:31:47.872Z", "type": "selectMarkdownCell" }, { "code": "There are two different ways to specify vis collections in Lux: 1) via Query or 2) via manual Vis construction.\n\nFirst, we see an example of a vis collection created in a similiar procedure as visualizations through the Query syntax. Here, we create a vis collection of `Weight` with respect to all other attributes.", "id": "f66167de4c96431fb571baa8f1131a28", "order_count": 10, "time": "2020-07-22T08:31:49.036Z", "type": "selectMarkdownCell" }, { "code": "There are two different ways to specify vis collections in Lux: 1) via Query or 2) via manual Vis construction.\n\nFirst, we see an example of a vis collection created in a similiar procedure as visualizations through the Query syntax. Here, we create a vis collection of `Weight` with respect to all other attributes.", "id": "f66167de4c96431fb571baa8f1131a28", "order_count": 10, "time": "2020-07-22T08:31:49.205Z", "type": "selectMarkdownCell" }, { "code": "There are two different ways to specify vis collections in Lux: 1) via Query or 2) via manual Vis construction.\n\nFirst, we see an example of a vis collection created in a similiar procedure as visualizations through the Query syntax. Here, we create a vis collection of `Weight` with respect to all other attributes.", "id": "f66167de4c96431fb571baa8f1131a28", "order_count": 10, "time": "2020-07-22T08:31:50.317Z", "type": "editMarkdownCell" }, { "code": "There are two different ways to specify vis collections in Lux: 1) via Query or 2) via manual Vis construction.\n\nFirst, we see an example of a vis collection created in a similiar procedure as visualizations through the Query syntax. Here, we create a vis collection of `Weight` with respect to all other attributes.", "id": "f66167de4c96431fb571baa8f1131a28", "order_count": 10, "time": "2020-07-22T08:31:52.048Z", "type": "selectMarkdownCell" }, { "code": "Using the Query syntax, we can make use of wildcards:", "id": "32586c7ef8d14b3982d51df0933e72a8", "order_count": 11, "time": "2020-07-22T08:32:03.689Z", "type": "selectMarkdownCell" }, { "time": "2020-07-22T08:32:49.388Z", "type": "kernelRestart" }, { "time": "2020-07-22T08:32:50.246Z", "type": "kernelReady" }, { "time": "2020-07-22T08:32:50.258Z", "type": "kernelReady" } ], "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 }