2Functions & classes that extend the plotly library
8def ridgeplot(df:
'DataFrame', x:str, y:str=
None, z:str=
None, dist:float=.5, overlap:float=0, **kwargs) -> go.Figure:
9 """ Create a ridgeplot in plotly.
10 `x` is the name of the column
in `df` that specifies the x data.
11 `y` can either be the name of the column of the y axis data,
if `df`
is in
12 long format,
or left unspecified
if `df`
is in wide format. If
in wide
13 format, it
's implied that the data in the columns specified by `z` are
15 `z` is either a list of columns of `y` data (the column names are then `z`)
16 if `df`
is in wide format,
or the column name
in `df` of the labels
17 that say what each sample belongs to,
if in long format.
18 `dist` dictates how close each `z`
is. 0 means the max value of each `z`
19 is the min value of the one above it,
and 1 means the min value of each
20 `z`
is the start of the shading of the `z` above it. Increasing this
21 also makes each `z` taller to compensate.
22 `overlap` effects the size of the shading directly. Usually between -1
27 import plotly.graph_objects
as go
32 if not isinstance(df, pl.DataFrame):
36 assert isinstance(z, (tuple, list))
44 array_dict[f
'x_{i}'] = df[x]
46 array_dict[f
'y_{i}'] = df[i]
47 array_dict[f
'y_{i}'] = (array_dict[f
'y_{i}'] - array_dict[f
'y_{i}'].min()) / (array_dict[f
'y_{i}'].max() - array_dict[f
'y_{i}'].min())
49 for i
in df[z].unique():
50 array_dict[f
'x_{i}'] = df.filter(pl.col(z) == i)[x]
52 array_dict[f
'y_{i}'] = df.filter(pl.col(z) == i)[y]
53 array_dict[f
'y_{i}'] = (array_dict[f
'y_{i}'] - array_dict[f
'y_{i}'].min()) / (array_dict[f
'y_{i}'].max() - array_dict[f
'y_{i}'].min())
59 for index, _z
in enumerate(zs):
60 fig.add_trace(go.Scatter(
65 y=np.full(2, len(zs)-index - overlap),
70 fig.add_trace(go.Scatter(
71 x=array_dict[f
'x_{_z}'],
72 y=array_dict[f
'y_{_z}'] + (len(zs)-index) + dist,
91 yaxis_showticklabels=
False,
go.Figure ridgeplot('DataFrame' df, str x, str y=None, str z=None, float dist=.5, float overlap=0, **kwargs)
Create a ridgeplot in plotly.