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")