Interactive Data Visualization¶

Feng Li

School of Statistics and Mathematics

Central University of Finance and Economics

feng.li@cufe.edu.cn

https://feng.li/python

Candlestick chart¶

  • The candlestick chart is a style of financial chart describing open, high, low and close for a given x coordinate (most likely time).

  • The boxes represent the spread between the open and close values and the lines represent the spread between the low and high values.

  • 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.

In [1]:
## This allows for saving the notebook as interactive html
import plotly.io as pio
pio.renderers.default='notebook'
In [2]:
import pandas as pd
df = pd.read_csv('data/finance-charts-apple.csv')
df
Out[2]:
Date AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted dn mavg up direction
0 2015-02-17 127.489998 128.880005 126.919998 127.830002 63152400 122.905254 106.741052 117.927667 129.114281 Increasing
1 2015-02-18 127.629997 128.779999 127.449997 128.720001 44891700 123.760965 107.842423 118.940333 130.038244 Increasing
2 2015-02-19 128.479996 129.029999 128.330002 128.449997 37362400 123.501363 108.894245 119.889167 130.884089 Decreasing
3 2015-02-20 128.619995 129.500000 128.050003 129.500000 48948400 124.510914 109.785449 120.763500 131.741551 Increasing
4 2015-02-23 130.020004 133.000000 129.660004 133.000000 70974100 127.876074 110.372516 121.720167 133.067817 Increasing
... ... ... ... ... ... ... ... ... ... ... ...
501 2017-02-10 132.460007 132.940002 132.050003 132.119995 20065500 132.119995 114.494004 124.498666 134.503328 Decreasing
502 2017-02-13 133.080002 133.820007 132.750000 133.289993 23035400 133.289993 114.820798 125.205166 135.589534 Increasing
503 2017-02-14 133.470001 135.089996 133.250000 135.020004 32815500 135.020004 115.175718 125.953499 136.731280 Increasing
504 2017-02-15 135.520004 136.270004 134.619995 135.509995 35501600 135.509995 115.545035 126.723499 137.901963 Decreasing
505 2017-02-16 135.669998 135.899994 134.839996 135.350006 22118000 135.350006 116.203299 127.504333 138.805366 Decreasing

506 rows × 11 columns

In [ ]:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'],
                high=df['AAPL.High'],
                low=df['AAPL.Low'],
                close=df['AAPL.Close'])])
fig.show()
In [17]:
import plotly.express as px

df = px.data.stocks(indexed=True)-1
fig = px.area(df, facet_col="company", facet_col_wrap=3)
fig.show()

Waterfall chart¶

  • 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.

  • 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.

  • Waterfall charts were popularized by the strategic consulting firm McKinsey & Company in its presentations to clients.

In [16]:
import plotly.graph_objects as go

fig = go.Figure(go.Waterfall(
    name = "20", orientation = "v",
    measure = ["relative", "relative", "total", "relative", "relative", "total"],
    x = ["Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax"],
    textposition = "outside",
    text = ["+60", "+80", "", "-40", "-20", "Total"],
    y = [60, 80, 0, -40, -20, 0],
    connector = {"line":{"color":"rgb(63, 63, 63)"}},
))

fig.update_layout(
        title = "Profit and loss statement 2018",
        showlegend = True
)

fig.show()

HeatMap plot¶

In [1]:
import pandas as pd
df = pd.read_csv('data/earthquakes-23k.csv')

import plotly.express as px
fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=6,
                        center=dict(lat=0, lon=180), zoom=0,
                        mapbox_style="stamen-terrain")
fig.show()
In [14]:
import plotly.express as px

df = pd.read_csv("data/gapminderDataFiveYear.csv")
df
Out[14]:
country year pop continent lifeExp gdpPercap
0 Afghanistan 1952 8425333.0 Asia 28.801 779.445314
1 Afghanistan 1957 9240934.0 Asia 30.332 820.853030
2 Afghanistan 1962 10267083.0 Asia 31.997 853.100710
3 Afghanistan 1967 11537966.0 Asia 34.020 836.197138
4 Afghanistan 1972 13079460.0 Asia 36.088 739.981106
... ... ... ... ... ... ...
1699 Zimbabwe 1987 9216418.0 Africa 62.351 706.157306
1700 Zimbabwe 1992 10704340.0 Africa 60.377 693.420786
1701 Zimbabwe 1997 11404948.0 Africa 46.809 792.449960
1702 Zimbabwe 2002 11926563.0 Africa 39.989 672.038623
1703 Zimbabwe 2007 12311143.0 Africa 43.487 469.709298

1704 rows × 6 columns

In [15]:
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

fig["layout"].pop("updatemenus") # optional, drop animation buttons
fig.show()
In [ ]: