Plotly Express

  • Plotly: インタラクティブなデータ可視化

  • Plotly Express: plotly.py の高レベル API、簡潔なコードによるデータ可視化

Ploty Expressのほうがかんたんに記述できるため、まずはPlotly Expressからはじめることをおすすめします

import pandas as pd
import plotly.express as px
btceur = pd.read_pickle("../data/binance_btc-eur.pkl")
etheur = pd.read_pickle("../data/binance_eth-eur.pkl")
def make_resample_df(df, rule):
    new_df = df.loc[:, "price"].resample(rule, label="right").ohlc()
    new_df.loc[:, "volume"] = df.loc[:, "size"].resample(rule, label="right").sum()
    new_df.reset_index(inplace=True)
    new_df.loc[:, "instrument"] = df.loc[df.index[0], "instrument"]
    new_df.loc[:, "pct_change"] = new_df.loc[:, "close"].pct_change()
    new_df.loc[:, "day_name"] = new_df.loc[:, "datetime"].dt.day_name()
    return new_df
resample_btceur = make_resample_df(btceur, "60min")
resample_etheur = make_resample_df(etheur, "60min")
resample = pd.concat([resample_btceur, resample_etheur])
resample_btceur.head()
datetime open high low close volume instrument pct_change day_name
0 2022-03-01 01:00:00+00:00 38500.00 39014.56 38500.00 38882.82 80.20571 btc-eur NaN Tuesday
1 2022-03-01 02:00:00+00:00 38883.49 38932.19 38580.47 38611.40 32.11602 btc-eur -0.006980 Tuesday
2 2022-03-01 03:00:00+00:00 38611.61 38642.27 38362.82 38525.47 47.45415 btc-eur -0.002226 Tuesday
3 2022-03-01 04:00:00+00:00 38525.47 38553.72 38275.12 38531.29 21.72403 btc-eur 0.000151 Tuesday
4 2022-03-01 05:00:00+00:00 38525.02 38738.82 38463.30 38690.69 42.37140 btc-eur 0.004137 Tuesday
resample_etheur.head()
datetime open high low close volume instrument pct_change day_name
0 2022-03-01 01:00:00+00:00 2605.42 2653.14 2604.59 2631.00 1539.8496 eth-eur NaN Tuesday
1 2022-03-01 02:00:00+00:00 2632.00 2632.88 2600.06 2603.31 587.8921 eth-eur -0.010525 Tuesday
2 2022-03-01 03:00:00+00:00 2603.42 2607.36 2592.52 2604.11 400.0061 eth-eur 0.000307 Tuesday
3 2022-03-01 04:00:00+00:00 2604.73 2604.73 2580.31 2599.68 266.5550 eth-eur -0.001701 Tuesday
4 2022-03-01 05:00:00+00:00 2599.68 2616.61 2594.24 2611.06 508.2370 eth-eur 0.004377 Tuesday

一般的なつかいかた

利用できる関数

  • Basics: scatter, line, area, bar, funnel, timeline

  • Part-of-Whole: pie, sunburst, treemap, icicle, funnel_area

  • 1D Distributions: histogram, box, violin, strip, ecdf

  • 2D Distributions: density_heatmap, density_contour

  • Matrix or Image Input: imshow

  • 3-Dimensional: scatter_3d, line_3d

  • Multidimensional: scatter_matrix, parallel_coordinates, parallel_categories

  • Tile Maps: scatter_mapbox, line_mapbox, choropleth_mapbox, density_mapbox

  • Outline Maps: scatter_geo, line_geo, choropleth

  • Polar Charts: scatter_polar, line_polar, bar_polar

  • Ternary Charts: scatter_ternary, line_ternary

引数

とりあえずは、これだけを覚えておけばOKです

  • data_frame: DataFrame

  • x: X値をとる列名

  • y: Y値をとる列名

px.line(resample_btceur, x="datetime", y="close")

引数 hover_data に列名を与えることで、ホバーツールに表示されます

px.line(resample_btceur, x="datetime", y="close", hover_name="datetime", hover_data=["volume", "pct_change", "day_name"])

ヒストグラムは引数 x または y のいずれかを指定します

px.histogram(resample_btceur, x="pct_change")
px.histogram(resample_btceur, y="pct_change")

色分け

引数 color に指定した列名で色分けできます

px.histogram(resample_btceur, x="pct_change", color="day_name")
px.box(resample_btceur, y="pct_change", color="day_name")
px.violin(resample_btceur, y="pct_change", color="day_name", box=True)

ファセット

データを分割し、そのデータをサブプロットに描画します

  • facet_row: 行で分割

  • facet_col: 列で分割

px.histogram(resample_btceur, x="pct_change", facet_col="day_name")
px.histogram(resample, x="pct_change", facet_col="day_name", facet_row="instrument")

ファセットと色分けを組み合わせられます

px.histogram(resample, x="pct_change", color="instrument", facet_col="day_name", opacity=0.5)
px.histogram(resample, x="pct_change", color="day_name", facet_col="instrument")
px.violin(resample, y="pct_change", color="day_name", box=True, facet_col="instrument")