Plotly 3D Cone Plot does not scale the colorbar with the magnitude (norm) of the cones (vectors)

Hello, is anyone here familiar with Plotly (R or Python). I am trying to use R plotly to visualize 3D cones (vectors). Below are my codes

library(plotly)
library(tidyverse)


## Data preparation ##


rotate_x <- function(vector, theta) {
    x = vector[1]
    y = vector[2]
    return(x*cos(theta) - y*sin(theta))
}

rotate_y <- function(vector, theta) {
    x = vector[1]
    y = vector[2]
    return(x*sin(theta) + y*cos(theta))
}

tail_1 = c(0, 0, 0)

tail_2 = c(1, 1, 1) * 0.5

tail_3 = c(
    rotate_x(tail_2, 2*pi/3),
    rotate_y(tail_2, 2*pi/3),
    tail_2[3]
)

tail_4 = c(
    rotate_x(tail_2, -2*pi/3),
    rotate_y(tail_2, -2*pi/3),
    tail_2[3]
)

four_cones <- matrix(
    c(tail_1, tail_2, tail_3, tail_4), 
    ncol = 3, 
    byrow = TRUE
) %>%
  as.data.frame()  %>%
  setNames(c("X", "Y", "Z")) %>%
  mutate(
    U = X * c(0, 1.5, 1.75, 2),
    V = Y * c(0, 1.5, 1.75, 2),
    W = Z + c(-2, 0.5, 0.75, 0.1)
)

print(four_cones)
#            X          Y   Z          U          V     W
# 1  0.0000000  0.0000000 0.0  0.0000000  0.0000000 -2.00
# 2  0.5000000  0.5000000 0.5  0.7500000  0.7500000  1.00
# 3 -0.6830127  0.1830127 0.5 -1.1952722  0.3202722  1.25
# 4  0.1830127 -0.6830127 0.5  0.3660254 -1.3660254  0.60



## Create 3D cone plot object using plot_ly() and add_trace(type = "cone") ##


multi_3Dcone <- plot_ly( # Mapping data
    data = four_cones,
    x = ~X, # x has 4 elements => 4 cones
    y = ~Y,
    z = ~Z
) %>%
  add_trace(
    type = "cone",
    u = ~U,
    v = ~V,
    w = ~W,
    anchor = "tail",
    sizemode = "raw",
    sizeref = 2,
    showscale = TRUE
)


## Display output ##


options(browser = "xdg-open") # For Linux
multi_3Dcone

The problem I encountered is that my 3D cone plot scales the color bar with the Z values, not with the magnitude (or norm) of the vectors (cones), like the picture I show here.

Can you help me to fix this? To make this plot scale the color with magnitude instead of the Z values ?

Welcome, @longistamina.

I’m not sure, in Python the desired behaviour seems to be the default, doesnt’t it?

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/cone_plot_data.csv"
)

fig = go.Figure(
    data=go.Cone(
        x=df["x"][:10],
        y=df["y"][:10],
        z=df["z"][:10],
        u=df["u"][:10],
        v=df["v"][:10],
        w=df["w"][:10],
        sizemode="raw",
        sizeref=0.1,
        colorscale="Portland",
        #cmin=0,
        #cmax=80,
        hoverinfo="u+v+w+text",
        text="-> wind <-",
        anchor = "tail",
    ),
)


fig.show()

data:

,u,v,w,x,y,z
0,0.56851,0.62783,-0.001017,70.188,17.5,-0.002
1,0.56851,0.62783,-0.001017,71.791,17.5,-0.002
2,0.56851,0.62783,-0.001017,73.393,17.5,-0.002
3,0.87909,0.62783,-0.001017,74.996,17.5,-0.002
4,0.56851,0.62783,-0.001017,76.599,17.5,-0.002
5,0.56851,0.62783,-0.001017,78.202,17.5,-0.002
6,0.56851,0.27732,-0.001017,79.805,17.5,-0.002
7,0.87909,-0.42369,-0.001017,81.407,17.5,-0.002
8,0.87909,-0.42369,-0.001017,83.01,17.5,-0.002
9,0.87909,0.27732,-0.001017,84.613,17.5,-0.002

Thank you for your reply @AIMPED.

When I ran some plotly examples like the 3D Cone Vortex, I realized that the colorbar actually scales with the norm (magnitude) of the vectors.

But somehow in my case, it does not… That’s why I am asking for support.

That’s completely understandable @longistamina. I just wanted to make sure everybody understood the the issue. Unfortunately I can’t help with R

1 Like