{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "

Visualizations of dataframes beyond simple tables

\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "import lux\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"https://github.com/lux-org/lux-datasets/blob/master/data/hpi_cleaned.csv?raw=True\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "

Steering analysis with intent

\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.intent = [\"Inequality\",\"AvrgLifeExpectancy\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "

Data Manipulation + Vis without changing a line of your Pandas code

\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "covid = pd.read_csv(\"https://github.com/lux-org/lux-datasets/blob/master/data/covid_cleaned.csv?raw=True\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = covid.merge(df,left_on=[\"Entity\",\"Code\"],right_on=[\"Country\",\"cca3\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 🤔 Some interesting findings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.intent = [\"Inequality\",\"AvrgLifeExpectancy\"]\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "

Exporting visualization insight to edit and share

\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.exported" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(df.exported[0].to_code(\"altair\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can copy-and-paste the output Altair code into a separate cell. Then let's tweak the plotting code a bit before sharing this insight with our colleagues." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import altair as alt\n", "\n", "c = \"#e7298a\"\n", "chart = alt.Chart(df,title=\"Check out this cool insight!\").mark_circle().encode(\n", " x=alt.X('Inequality',scale=alt.Scale(domain=(0.04, 0.51)),type='quantitative', axis=alt.Axis(title='Inequality')),\n", " y=alt.Y('AvrgLifeExpectancy',scale=alt.Scale(domain=(48.9, 83.6)),type='quantitative', axis=alt.Axis(title='AvrgLifeExpectancy'))\n", ")\n", "highlight = df[(df[\"Inequality\"]>0.35)&(df[\"stringency_level\"]==\"High\")]\n", "\n", "hchart = alt.Chart(highlight).mark_point(color=c,size=50,shape=\"cross\").encode(\n", " x=alt.X('Inequality',scale=alt.Scale(domain=(0.04, 0.51)),type='quantitative', axis=alt.Axis(title='Inequality')),\n", " y=alt.Y('AvrgLifeExpectancy',scale=alt.Scale(domain=(48.9, 83.6)),type='quantitative', axis=alt.Axis(title='AvrgLifeExpectancy')),\n", ")\n", "\n", "text = alt.Chart(highlight).mark_text(color=c,dx=-35,dy=0,fontWeight=800).encode(\n", " x=alt.X('Inequality',scale=alt.Scale(domain=(0.04, 0.51)),type='quantitative', axis=alt.Axis(title='Inequality')),\n", " y=alt.Y('AvrgLifeExpectancy',scale=alt.Scale(domain=(48.9, 83.6)),type='quantitative', axis=alt.Axis(title='AvrgLifeExpectancy')),\n", " text=alt.Text('Country')\n", ")\n", "\n", "chart = chart.encode(color=alt.Color('stringency_level',type='nominal'))\n", "chart = chart.properties(width=160,height=150)\n", "\n", "(chart + hchart + text).configure_title(color=c)\n" ] } ], "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.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }