Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cones color problems with Plotly

Tags:

python

plotly

I am having a small issue when plotting cones with Plotly. In my data, I have 2 types of vectors for norm 1 and 1.5. When I plot just 8 of them on a cube, the colors are rendered very well with each type of vector have one of two colors. But as soon as I go bigger with 2 cubes for example then it becomes quite random. Here is a minimal example that reproduces this issue.

The data:

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'])

Plotting 1 cube:

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

The figure displays the correct color for the 8 vectors.

Plotting 2 cubes:

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

As you can see on this last image, all vectors of norm 1 are dark purple, whereas the vectors of norm 1.5 take a gradient of colors with some even being dark purple as shown by the cone that has its coordinates displayed.

Does anyone know how to solve this issue?

like image 746
Karim Avatar asked Aug 15 '26 08:08

Karim


1 Answers

This is probably not a helpful answer, but the best I can make out is you've stumbled on to a bug. I just updated to the latest (plotly 4.11.0) and I get the same results.

If you pass in a constant for u,v,w it renders correctly:

# 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

So then I was thinking that maybe the data was getting misaligned (input df's are not always the same size, also side note pos2 is not used)... BUT, even with your data extracted and put into vortexTest.csv I get similar results:

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

It looks like the norm is getting calculated correctly, but somehow the color is not applied right. As you've pointed out there should only be two colors here, yellow for 1.5 and blue for 1.0.

like image 104
jayveesea Avatar answered Aug 16 '26 22:08

jayveesea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!