python - Plotly 的锥体颜色问题

标签 python plotly

我在使用 Plotly 绘制圆锥体时遇到了一个小问题。在我的数据中,范数 1 和 1.5 有两种类型的向量。当我在一个立方体上绘制其中的 8 个时,颜色呈现得非常好,每种类型的矢量都具有两种颜色之一。但是一旦我用 2 个立方体变大,它就会变得非常随机。这是重现此问题的最小示例。

数据:

import numpy as np
import pandas as pd
import plotly.graph_objects as go

pos=pd.DataFrame([
      [0.0,0.0,0.0],
      [0.5,0.0,0.0],
      [0.0, 0.5,0.0],
      [0.5,0.5,0.0],
      [0.0,0.0,0.5],
      [0.5,0.0,0.5],
      [0.0,0.5,0.5],
      [0.5,0.5,0.5]
      ], columns=['x','y','z'])
pos2=pd.DataFrame([
      [0.0,0.0,0.0],
      [0.5,0.0,0.0],
      [0.0, 0.5,0.0],
      [0.5,0.5,0.0],
      [0.0,0.0,0.5],
      [0.5,0.0,0.5],
      [0.0,0.5,0.5],
      [0.5,0.5,0.5],
      [1.0,0.0,0.0],
      [1.5,0.0,0.0],
      [1.0, 0.5,0.0],
      [1.5,0.5,0.0],
      [1.0,0.0,0.5],
      [1.5,0.0,0.5],
      [1.0,0.5,0.5],
      [1.5,0.5,0.5]
      ], columns=['x','y','z'])
type = pd.DataFrame(np.array([[1],[1.5],[1],[1.5],[1],[1.5],[1],[1.5],[1],[1.5],[1],[1.5],[1],[1.5],[1],[1.5]]), columns=['Type'])
vec = pd.DataFrame(np.array([[ 0.56493151,  0.58643612, -0.5804697 ],
       [ 0.52637789, -0.81556709, -0.24036772],
       [-0.64603163, -0.4828808 ,  0.59115925],
       [-0.42559098,  0.83496509,  0.34886332],
       [ 0.16788166,  0.63197593,  0.75658587],
       [ 0.8961372 , -0.20426566,  0.39397165],
       [-0.08599516, -0.68074558, -0.72745467],
       [-0.90366508,  0.25896575, -0.34106622],
       [ 0.68002417,  0.47929612, -0.55483543],
       [ 0.2721998 ,  0.70224966, -0.65783941],
       [-0.68464335, -0.51281615,  0.5179605 ],
       [-0.35330142, -0.53106658,  0.77015998],
       [-0.46760187,  0.29858559,  0.83198265],
       [ 0.68459979, -0.50165276,  0.52883612],
       [ 0.44722097, -0.32492581, -0.83331664],
       [-0.55474541,  0.49785322, -0.66663311]]), columns=['x','y','z'])

绘制 1 个立方体:

layout = go.Layout(
         title='Cones',
         width=700,
         height=700
        )

fig = go.Figure(data = go.Cone(
    x=pos['x'],
    y=pos['y'],
    z=pos['z'],
    u=type['Type']*vec['x'],
    v=type['Type']*vec['y'],
    w=type['Type']*vec['z'],
    colorscale='viridis',
    colorbar=dict(thickness=20, ticklen=4),
    sizemode="absolute",
    sizeref=0.5,
    anchor='tail'
), layout=layout)

fig

enter image description here

图中显示了 8 个向量的正确颜色。

绘制 2 个立方体:

layout = go.Layout(
         title='Cones',
         width=700,
         height=700
        )

fig = go.Figure(data = go.Cone(
    x=pos.append(pos+[1,0,0], ignore_index=True)['x'],
    y=pos.append(pos+[1,0,0], ignore_index=True)['y'],
    z=pos.append(pos+[1,0,0], ignore_index=True)['z'],
    u=type['Type']*vec['x'],
    v=type['Type']*vec['y'],
    w=type['Type']*vec['z'],
    colorscale='viridis',
    colorbar=dict(thickness=20, ticklen=4),
    sizemode="absolute",
    sizeref=0.5,
    anchor='tail'
), layout=layout)

fig

enter image description here

正如您在最后一张图片中所看到的,范数 1 的所有向量都是深紫色,而范数 1.5 的向量采用颜色渐变,有些甚至是深紫色,如显示其坐标的圆锥体所示。

有谁知道如何解决这个问题?

最佳答案

这可能不是一个有帮助的答案,但我能确定的是您偶然发现了一个错误。我刚刚更新到最新版本(plotly 4.11.0),我得到了相同的结果。

如果您为 uvw 传递常量,它会正确呈现:

# using pos df from above
vect=pd.DataFrame(np.full((16, 3), 0.866),columns=['u','v','w'])

fig = go.Figure(data = go.Cone(
                    x=pos.append(pos+[1,0,0], ignore_index=True)['x'],
                    y=pos.append(pos+[1,0,0], ignore_index=True)['y'],
                    z=pos.append(pos+[1,0,0], ignore_index=True)['z'],
                    u=vect['u'],
                    v=vect['v'],
                    w=vect['w'],
                ))

fig.show()

enter image description here

然后我在想可能是数据未对齐(输入 df 的大小并不总是相同,也未使用旁注 pos2)...但是,即使您的数据已提取并放入 vortexTest.csv 我得到类似的结果:

x,      y,      z,      u,          v,          w
0,      0,      0,      0.564932,   0.586436,   -0.58047
0.5,    0,      0,      0.789567,   -1.223351,  -0.360552
0,      0.5,    0,      -0.646032,  -0.482881,  0.591159
0.5,    0.5,    0,      -0.638386,  1.252448,   0.523295
0,      0,      0.5,    0.167882,   0.631976,   0.756586
0.5,    0,      0.5,    1.344206,   -0.306398,  0.590957
0,      0.5,    0.5,    -0.085995,  -0.680746,  -0.727455
0.5,    0.5,    0.5,    -1.355498,  0.388449,   -0.511599
1,      0,      0,      0.680024,   0.479296,   -0.554835
1.5,    0,      0,      0.4083,     1.053374,   -0.986759
1,      0.5,    0,      -0.684643,  -0.512816,  0.517961
1.5,    0.5,    0,      -0.529952,  -0.7966,    1.15524
1,      0,      0.5,    -0.467602,  0.298586,   0.831983
1.5,    0,      0.5,    1.0269,     -0.752479,  0.793254
1,      0.5,    0.5,    0.447221,   -0.324926,  -0.833317
1.5,    0.5,    0.5,    -0.832118,  0.74678,    -0.99995
df= pd.read_csv("vortexTest.csv")

fig2 = go.Figure(data = go.Cone(
    x=df['x'], y=df['y'], z=df['z'],
    u=df['u'], v=df['v'], w=df['w'],
    cmax=1.5,
    cmin=1
))

fig2.show()

enter image description here

看起来 norm 计算正确,但不知何故颜色应用不正确。正如您所指出的,这里应该只有两种颜色,黄色代表 1.5,蓝色代表 1.0。

关于python - Plotly 的锥体颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64174744/

相关文章:

python - 在数据框中删除一行[有文本而不是数字[可能很简单](pandas、dataframe、python)

r - plotly :平行坐标 plotly :轴样式

python - 如何在 Plotly 的 plotly 内定位图例

python - 使用 python 发送电子邮件和错误消息不断弹出

python - 有没有办法在 plotly 中使用时间轴动画分组条形图?

R Shiny Plotly 3D : Change the title of x , y , z 轴到实际变量名称并添加图例标题

r - Plotly 中的缩放问题,R 编程

python - 用元组列表填充数据框列

Python - 将所有 'available' 元素保存在 Json 中,并在所有搜索完成后打印它?

python - python下sqlite3转换为mysql