{ "cells": [ { "cell_type": "markdown", "id": "ec9be3b3", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Interactive Data Visualization\n", "\n", "Feng Li\n", "\n", "School of Statistics and Mathematics\n", "\n", "Central University of Finance and Economics\n", "\n", "[feng.li@cufe.edu.cn](mailto:feng.li@cufe.edu.cn)\n", "\n", "[https://feng.li/python](https://feng.li/python)" ] }, { "cell_type": "markdown", "id": "a24064f7", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Candlestick chart\n", "\n", "- The candlestick chart is a style of financial chart describing open, high, low and close for a given x coordinate (most likely time). \n", "\n", "- The boxes represent the spread between the open and close values and the lines represent the spread between the low and high values. \n", "\n", "- Sample points where the close value is higher (lower) then the open value are called increasing (decreasing). By default, increasing candles are drawn in green whereas decreasing are drawn in red." ] }, { "cell_type": "code", "execution_count": 1, "id": "7d79d1cb", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "## This allows for saving the notebook as interactive html\n", "import plotly.io as pio\n", "pio.renderers.default='notebook'" ] }, { "cell_type": "code", "execution_count": 2, "id": "3e8c4998", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateAAPL.OpenAAPL.HighAAPL.LowAAPL.CloseAAPL.VolumeAAPL.Adjusteddnmavgupdirection
02015-02-17127.489998128.880005126.919998127.83000263152400122.905254106.741052117.927667129.114281Increasing
12015-02-18127.629997128.779999127.449997128.72000144891700123.760965107.842423118.940333130.038244Increasing
22015-02-19128.479996129.029999128.330002128.44999737362400123.501363108.894245119.889167130.884089Decreasing
32015-02-20128.619995129.500000128.050003129.50000048948400124.510914109.785449120.763500131.741551Increasing
42015-02-23130.020004133.000000129.660004133.00000070974100127.876074110.372516121.720167133.067817Increasing
....................................
5012017-02-10132.460007132.940002132.050003132.11999520065500132.119995114.494004124.498666134.503328Decreasing
5022017-02-13133.080002133.820007132.750000133.28999323035400133.289993114.820798125.205166135.589534Increasing
5032017-02-14133.470001135.089996133.250000135.02000432815500135.020004115.175718125.953499136.731280Increasing
5042017-02-15135.520004136.270004134.619995135.50999535501600135.509995115.545035126.723499137.901963Decreasing
5052017-02-16135.669998135.899994134.839996135.35000622118000135.350006116.203299127.504333138.805366Decreasing
\n", "

506 rows × 11 columns

\n", "
" ], "text/plain": [ " Date AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume \\\n", "0 2015-02-17 127.489998 128.880005 126.919998 127.830002 63152400 \n", "1 2015-02-18 127.629997 128.779999 127.449997 128.720001 44891700 \n", "2 2015-02-19 128.479996 129.029999 128.330002 128.449997 37362400 \n", "3 2015-02-20 128.619995 129.500000 128.050003 129.500000 48948400 \n", "4 2015-02-23 130.020004 133.000000 129.660004 133.000000 70974100 \n", ".. ... ... ... ... ... ... \n", "501 2017-02-10 132.460007 132.940002 132.050003 132.119995 20065500 \n", "502 2017-02-13 133.080002 133.820007 132.750000 133.289993 23035400 \n", "503 2017-02-14 133.470001 135.089996 133.250000 135.020004 32815500 \n", "504 2017-02-15 135.520004 136.270004 134.619995 135.509995 35501600 \n", "505 2017-02-16 135.669998 135.899994 134.839996 135.350006 22118000 \n", "\n", " AAPL.Adjusted dn mavg up direction \n", "0 122.905254 106.741052 117.927667 129.114281 Increasing \n", "1 123.760965 107.842423 118.940333 130.038244 Increasing \n", "2 123.501363 108.894245 119.889167 130.884089 Decreasing \n", "3 124.510914 109.785449 120.763500 131.741551 Increasing \n", "4 127.876074 110.372516 121.720167 133.067817 Increasing \n", ".. ... ... ... ... ... \n", "501 132.119995 114.494004 124.498666 134.503328 Decreasing \n", "502 133.289993 114.820798 125.205166 135.589534 Increasing \n", "503 135.020004 115.175718 125.953499 136.731280 Increasing \n", "504 135.509995 115.545035 126.723499 137.901963 Decreasing \n", "505 135.350006 116.203299 127.504333 138.805366 Decreasing \n", "\n", "[506 rows x 11 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv('data/finance-charts-apple.csv')\n", "df" ] }, { "cell_type": "code", "execution_count": 3, "id": "90397b0d", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.graph_objects as go\n", "fig = go.Figure(data=[go.Candlestick(x=df['Date'],\n", " open=df['AAPL.Open'],\n", " high=df['AAPL.High'],\n", " low=df['AAPL.Low'],\n", " close=df['AAPL.Close'])])\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": 4, "id": "6172534c", "metadata": { "scrolled": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.express as px\n", "\n", "df = px.data.stocks(indexed=True)-1\n", "fig = px.area(df, facet_col=\"company\", facet_col_wrap=3)\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "6ed8939e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Waterfall chart\n", "\n", "- A waterfall chart is a form of data visualization that helps in understanding the cumulative effect of sequentially introduced positive or negative values. These intermediate values can either be time based or category based. \n", "\n", "- The waterfall chart is also known as a flying bricks chart or Mario chart due to the apparent suspension of columns (bricks) in mid-air. Often in finance, it will be referred to as a bridge.\n", "\n", "- Waterfall charts were popularized by the strategic consulting firm McKinsey & Company in its presentations to clients." ] }, { "cell_type": "code", "execution_count": 5, "id": "0cefcf6b", "metadata": { "scrolled": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.graph_objects as go\n", "\n", "fig = go.Figure(go.Waterfall(\n", " name = \"20\", orientation = \"v\",\n", " measure = [\"relative\", \"relative\", \"total\", \"relative\", \"relative\", \"total\"],\n", " x = [\"Sales\", \"Consulting\", \"Net revenue\", \"Purchases\", \"Other expenses\", \"Profit before tax\"],\n", " textposition = \"outside\",\n", " text = [\"+60\", \"+80\", \"\", \"-40\", \"-20\", \"Total\"],\n", " y = [60, 80, 0, -40, -20, 0],\n", " connector = {\"line\":{\"color\":\"rgb(63, 63, 63)\"}},\n", "))\n", "\n", "fig.update_layout(\n", " title = \"Profit and loss statement 2018\",\n", " showlegend = True\n", ")\n", "\n", "fig.show()" ] }, { "cell_type": "markdown", "id": "8057074b", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## HeatMap plot" ] }, { "cell_type": "code", "execution_count": 6, "id": "dc16dd83", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv('data/earthquakes-23k.csv')\n", "\n", "import plotly.express as px\n", "fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=6,\n", " center=dict(lat=0, lon=180), zoom=0,\n", " mapbox_style=\"stamen-terrain\")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": 7, "id": "b7119bfa", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countryyearpopcontinentlifeExpgdpPercap
0Afghanistan19528425333.0Asia28.801779.445314
1Afghanistan19579240934.0Asia30.332820.853030
2Afghanistan196210267083.0Asia31.997853.100710
3Afghanistan196711537966.0Asia34.020836.197138
4Afghanistan197213079460.0Asia36.088739.981106
.....................
1699Zimbabwe19879216418.0Africa62.351706.157306
1700Zimbabwe199210704340.0Africa60.377693.420786
1701Zimbabwe199711404948.0Africa46.809792.449960
1702Zimbabwe200211926563.0Africa39.989672.038623
1703Zimbabwe200712311143.0Africa43.487469.709298
\n", "

1704 rows × 6 columns

\n", "
" ], "text/plain": [ " country year pop continent lifeExp gdpPercap\n", "0 Afghanistan 1952 8425333.0 Asia 28.801 779.445314\n", "1 Afghanistan 1957 9240934.0 Asia 30.332 820.853030\n", "2 Afghanistan 1962 10267083.0 Asia 31.997 853.100710\n", "3 Afghanistan 1967 11537966.0 Asia 34.020 836.197138\n", "4 Afghanistan 1972 13079460.0 Asia 36.088 739.981106\n", "... ... ... ... ... ... ...\n", "1699 Zimbabwe 1987 9216418.0 Africa 62.351 706.157306\n", "1700 Zimbabwe 1992 10704340.0 Africa 60.377 693.420786\n", "1701 Zimbabwe 1997 11404948.0 Africa 46.809 792.449960\n", "1702 Zimbabwe 2002 11926563.0 Africa 39.989 672.038623\n", "1703 Zimbabwe 2007 12311143.0 Africa 43.487 469.709298\n", "\n", "[1704 rows x 6 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.express as px\n", "\n", "df = pd.read_csv(\"data/gapminderDataFiveYear.csv\")\n", "df" ] }, { "cell_type": "code", "execution_count": 8, "id": "f919cf84", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig = px.scatter(df, x=\"gdpPercap\", y=\"lifeExp\", animation_frame=\"year\", animation_group=\"country\",\n", " size=\"pop\", color=\"continent\", hover_name=\"country\",\n", " log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])\n", "\n", "fig[\"layout\"].pop(\"updatemenus\") # optional, drop animation buttons\n", "fig.show()" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.9" }, "rise": { "auto_select": "first", "autolaunch": false, "enable_chalkboard": true, "start_slideshow_at": "selected", "theme": "white" } }, "nbformat": 4, "nbformat_minor": 5 }