Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cones color issue #2827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
essafik opened this issue Oct 12, 2020 · 3 comments
Closed

Cones color issue #2827

essafik opened this issue Oct 12, 2020 · 3 comments

Comments

@essafik
Copy link

essafik commented Oct 12, 2020

I am having a small issue when plotting cones with Plotly (offline). 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

W4XKM

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

PFLnR

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?

@empet
Copy link

empet commented Oct 12, 2020

The Cone trace with vector norms colormapped to a colorscale is destinated mainly for plotting physical (differentiable) vector fields. The sizemode and sizeref set the mode by which the cones are sized.

Their definition can be read here: https://github.com/plotly/plotly.js/blob/master/src/traces/cone/attributes.js#L107.
It reveals that vector norms are multiplied by an internally computed factor:

This factor (computed internally) corresponds to the minimum "time" to travel across,
two successive x/y/z positions at the average velocity of those two successive positions

It's not clear what are two consecutive positions. Two positions having consecutive indices in the x, y, z arrays or two consecutive positions in space (but what really two consecutive positions in space mean?).

Here is a Jupyter Notebook, edited in May 2018, when Cone trace was introduced (Plotly version 2.7), and updated today using new Plotly style. All vector fields from the above notebook are colormapped in concordance with their norms (no odd color assigned as in your second plot). This makes me think that in your second case plotly.js couldn't derive correctly that internal factor, because your vectors are just some vectors placed somewhere in space, and no two vectors can be considered consecutive in space. In your first example you have two groups of vectors some at x=0 and others at x=0.5, and perhaps the two groups have been interpreted as "consecutive".

The internally computed factor was also discussed in this plotly.js issue plotly/plotly.js#3613.

Hence if you only played with this trace and noticed the reported issue, but you are interested in plotting a vector field whose vectors seem to follow a trajectory as in the examples from the above notebook, then you can try plot it and give a feedback here.

@essafik
Copy link
Author

essafik commented Oct 16, 2020

@empet Thank you for this insight! I will have a look at the discussed issue you mention.

I just tried something by adding an extra cube along the y- or z-directions and the issue remains the same. However, if I add one along both directions then it works as expected.

Here are the corresponding figures
Along y
newplot(1)

Along z
newplot(3)

Along both
newplot(2)

@gvwilson
Copy link
Contributor

Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants