{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import lux\n", "\n", "df = pd.read_csv('https://github.com/lux-org/lux-datasets/blob/master/data/olympic.csv?raw=true')\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. We will be working with the [Medals dataset](https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results), which contains information about Olympic medalists and associated information, such as the type of medals they won, their nationality, the Year in which they won the medal." ] }, { "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 `Vis`, 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": [ "vis.intent = [lux.Clause(\"Weight\",bin_size=25)]\n", "vis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Excercise 2.1: \n", "\n", "Plot a visualization that shows the number of athletes from different regions across winter v.s. summer olympics." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write code for plotting the desired Vis \n", "# Hint: Inspect the dataframe columns to identify what are the relevant attributes\n", "\n" ] }, { "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": [ "We can create a vis collection of `Weight` with respect to all other attributes, using the [wildcard](https://lux-api.readthedocs.io/en/latest/source/guide/intent.html#specifying-wildcards) \"?\" 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": [ "### Excercise 2.2: \n", "\n", "Plot a list of visualizations showing how physical characteristics of athletes (e.g., `Age`, `Weight`, and `Height`) changes over time. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Write code for generating the desired VisList\n", "# Hint: The `Year` attribute contains information about changes over time\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bonus Excercise: \n", "\n", "`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", "Can you rewrite the solution to the previous excercise by constructing a `VisList` using a list of `Vis` inputs?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now try rewriting this code for by creating individual Vis objects\n", "# Hint: Remember VisList is simply a list of Vis objects\n", "\n" ] } ], "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }