为plotly 3d曲面图将列转换为四维数据

2024-10-03 23:27:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图重塑我的数据,以在plotly中获得三维曲面图。 我有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)

有人知道如何在曲面函数中转换数据并正确地将其属性化吗? 提前谢谢


Tags: columns数据truedfdataindexvaluefig