首页 > 解决方案 > 将列转换为 4 维数据以绘制 3d 曲面图

问题描述

我尝试重塑我的数据以绘制 3d 曲面图。我有 4 列,我想将它们用于 3 轴,将第 4 列用于表面颜色。但我正在努力让数据以正确的形式出现。

到目前为止,我已经完成了前 3 个维度的工作:

def plotly_4d(data):
    df1 = pd.DataFrame.from_records(data, columns=['selcperc', 'mperiod', 'rocperiod', 'value'])
    df = df1[~(df1['selcperc'].isin([2]))]
    df= df.pivot_table(index=['mperiod'], columns=['rocperiod'], values='value').reset_index()
    del df['mperiod']
    df.drop(df.index[0])
    print(df)
    fig = go.Figure(data=[go.Surface(z=df.values)])
    fig.update_traces(contours_z=dict(show=True, usecolormap=True,
                                      highlightcolor="limegreen", project_z=True))
    fig.update_layout(title_text="Analysis of Strategy", autosize=False, width=700, height=700,
                  margin=dict(l=65, r=50, b=65, t=90))
    fig.show() 

data = [[1, 100, 100, 39.75640778351649], [1, 100, 110, 9.872282288628153], [1, 100, 120, 9.872282288628153], [1, 100, 130, 9.642593626259359], [1, 110, 100, 8.080838244365983], [1, 110, 110, 8.080838244365983], [1, 110, 120, 8.080838244365983], [1, 110, 130, 7.800644723300946], [1, 120, 100, 16.665716278906633], [1, 120, 110, 16.665716278906633], [1, 120, 120, 16.665716278906633], [1, 120, 130, 16.24384694969096], [1, 130, 100, 5.48409659630341], [1, 130, 110, 5.48409659630341], [1, 130, 120, 5.48409659630341], [1, 130, 130, 5.48409659630341], [
    2, 100, 100, 28.07034496404147], [2, 100, 110, 2.4858848187807294], [2, 100, 120, 2.4858848187807294], [2, 100, 130, 2.5146638062505695], [2, 110, 100, 2.2346947747962775], [2, 110, 110, 2.2346947747962775], [2, 110, 120, 2.2346947747962775], [2, 110, 130, 2.257673458249152], [2, 120, 100, 1.3453241481836469], [2, 120, 110, 1.3453241481836469], [2, 120, 120, 1.3453241481836469], [2, 120, 130, 1.3663313544445679], [2, 130, 100, 0.9013560706526865], [2, 130, 110, 0.9013560706526865], [2, 130, 120, 0.9013560706526865], [2, 130, 130, 0.9013560706526865]]

plotly_4d(data)

有谁知道如何在表面函数中转换数据并将其正确归因?提前致谢!

标签: pythonmatrix3dplotlysurface

解决方案


推荐阅读