28  Plotly

The very basics of plotly

import plotly.express as px
import numpy as np
import pandas as pd

28.1 Boxplot

x = np.random.randn(300)
y = np.random.randn(400) + 1.6
z = np.random.randn(320) - 2.2
px.box(y=x)
dat = pd.DataFrame({
    "key": ["x"]* 300 + ["y"] * 400 + ["z"] * 320,
    "val": np.concatenate((x, y, z))
})
dat.head()
key val
0 x -0.719056
1 x -1.598678
2 x 0.265365
3 x 1.219989
4 x 0.336870
px.box(dat, x="key", y="val")

28.2 Histogram

px.histogram(x)
px.histogram(dat, x="val", color="key")

28.3 Scatter

x = np.random.randn(300);
y = 3 + x**3 + np.random.randn(300);
y2 = 4 + x**2 + np.random.randn(300);
px.scatter(x=x, y=y)
px.scatter(x=x, y=[y, y2])

28.4 Barplot

x = np.array(["alpha", "beta", "gamma"])
y = np.array([3, 9, 5])
y2 = np.array([5, 8, 7])
px.bar(x=x, y=y)

29 Resources