{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Styling Custom Plot Settings " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import lux" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Collecting basic usage statistics for Lux (For more information, see: https://tinyurl.com/logging-consent)\n", "lux.logger = True # Remove this line if you do not want your interactions recorded" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"../data/car.csv\")\n", "df[\"Year\"] = pd.to_datetime(df[\"Year\"], format='%Y') # change pandas dtype for the column \"Year\" to datetype\n", "lux.config.default_display=\"lux\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the last tutorial, we saw how `Vis` objects could be exported into visualization code for further editing. What if we want to change the chart settings for *all* the visualizations displayed in the widget. In Lux, we can change the chart settings and aesthetics by inputting custom plot settings the `plot_config` property of the dataframe." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example #1 : Changing Color and Title of all charts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we've loaded in the [Cars dataset](http://lib.stat.cmu.edu/datasets/) and the visualizations recommended in the widget are in its default settings." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, visualizations in Lux are rendered using the [Altair](https://altair-viz.github.io/index.html) library.\n", "To change the plot configuration in Altair, we need to specify a function that takes an [AltairChart](https://altair-viz.github.io/user_guide/generated/toplevel/altair.Chart.html?highlight=chart) as input, performs some chart configurations in Altair, and returns the chart object as an output.\n", "\n", "Let's say that we want to change all the graphical marks of the charts to green and add a custom title. We can define this `change_color_add_title` function, which configures the chart's mark as green and adds a custom title to the chart." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def change_color_add_title(chart):\n", " chart = chart.configure_mark(color=\"green\") # change mark color to green\n", " chart.title = \"Custom Title\" # add title to chart\n", " return chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We then set the plot configuration of the dataframe by changing the `plot_config` property. With the added plot_config, Lux runs this user-defined function after every `Vis` is rendered to a chart, allow the user-defined function to override any existing default chart settings." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.plot_config = change_color_add_title" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now see that the displayed visualizations adopt these new imported settings." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we click on the visualization for `Displacement` v.s. `Weight` and export it. We see that the exported chart now contains code with these additional plot settings at the every end." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Before running this cell, click on Displacement v.s. Weight vis and export it.\n", "vis = df.exported[0]\n", "print (vis.to_Altair())" ] }, { "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('Weight',scale=alt.Scale(domain=(1613, 5140)),type='quantitative'),\n", " y=alt.Y('Displacement',scale=alt.Scale(domain=(68.0, 455.0)),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", "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 = chart.configure_mark(color=\"green\") # change mark color to green\n", "chart.title = \"Custom Title\" # add title to chart\n", "chart" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example #2: Changing Selected Chart Setting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we look at an example of customizing the chart setting for only selected sets of visualizations. \n", "\n", "Here, we load in the [Olympics dataset](https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results) and see that the recommended visualization is cluttered with many datapoints." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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\n", "lux.config.default_display=\"lux\"\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to decrease the opacity of scatterplots, but keep the opacity for the other types of visualization as default." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def changeOpacityScatterOnly(chart):\n", " if chart.mark=='circle':\n", " chart = chart.configure_mark(opacity=0.1) # lower opacity\n", " return chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.plot_config = changeOpacityScatterOnly\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can modify the scatterplot setting, without changing the settings for the other chart types." ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 4 }