具有连续和分类数据的平行坐标图

Parallel Coordinate plot in plotly with continuous and categorical data

假设我有一些数据框 df,其中包含连续的 分类数据。现在我想在 plotly 中绘制一个包含两种类型坐标的平行坐标图。是否可以将这些组合成一个图,使每个数据点线穿过 所有 轴?

在文档中我确实找到了 go.Parcoordsgo.Parcats 分别处理它们,但我没有找到将它们组合起来的方法。 这是我的最小示例:

import pandas as pd
import plotly.graph_objs as go
df = pd.DataFrame()
# continuous data
df['x1'] = [1,2,3,4]
df['x2'] = [9,8,7,6]
# categorical data
df['x3'] = ['a', 'b', 'b', 'c']
df['x4'] = ['A', 'B', 'C', 'C']
col_list = [dict(range=(df[col].min(), df[col].max()),
                 label=col,
                 values=df[col])
            for col in df.keys()
            #if col not in ['x3', 'x4']  # only works if we exclude these (uncomment to run)
            ]
fig = go.Figure(data=go.Parcoords(dimensions=col_list))
fig.show()

这是一个基于自定义刻度名称的解决方案 (ticktext)。首先我们用一个整数替换每个分类值,然后我们用相应的分类值定义我们的自定义刻度作为一个字符串:

import pandas as pd
import plotly.graph_objs as go
df = pd.DataFrame()
df['x1'] = [1,2,3,4]
df['x2'] = [9,8,7,6]
df['x3'] = ['a', 'b', 'b', 'c']
df['x4'] = ['A', 'B', 'C', 'C']
keys = df.keys()
categorical_columns = ['x3', 'x4']
col_list = []

for col in df.keys():
    if col in categorical_columns:  # categorical columns
        values = df[col].unique()
        value2dummy = dict(zip(values, range(len(values))))  # works if values are strings, otherwise we probably need to convert them
        df[col] = [value2dummy[v] for v in df[col]]
        col_dict = dict(
            label=col,
            tickvals=list(value2dummy.values()),
            ticktext=list(value2dummy.keys()),
            values=df[col],
        )
    else:  # continuous columns
        col_dict = dict(
            range=(df[col].min(), df[col].max()),
            label=col,
            values=df[col],
        )
    col_list.append(col_dict)
fig = go.Figure(data=go.Parcoords(dimensions=col_list))
fig.show()