Willy Menacho Nacho
Willy Menacho Nacho

Reputation: 31

How to assign colors in go.Cone based on a single column of a DataFrame instead of the total magnitude calculated by default?

I’m working with Plotly’s go.Cone function to visualize 3D data. In my plot, I’m representing cones with positions (x, y, z) and directions (u, v, w), where the components u, v, and w correspond to forces in each direction. Currently, the color bar in the plot is based on the total magnitude of the force vectors (sqrt(u^2 + v^2 + w^2)) by default.

However, since I normalize the forces to keep the cones the same size, this default color scale is not suitable as all values end up being less than or equal to 1. What I would like is for the color bar to reflect only the values from a specific column in the DataFrame that I use, not the combined magnitude. In other words, I want each cone to be colored according to a pre-calculated value in the DataFrame at its position, without considering the default magnitude.

I normalized the forces because previously there were issues with magnitude, resulting in a few large cones and many too small, as shown in Figure 1:

enter image description here

The resulting image using normalized data is shown below: enter image description here

Here’s the code I’m using:

fig = go.Figure(data=go.Cone(
    x=df[“x”], y=df[“y”], z=df[“z”],
    u=df[“Force_x”]/df[“Force_Mag”], v=df[“Force_y”]/df[“Force_Mag”], w=df[“Force_z”]/df[“Force_Mag”],
    colorscale=‘Viridis’, colorbar=dict(title=‘Force Mag’),
    hovertext=df[“Aminoacido”], # text for info
    hoverinfo=“x+y+z+text+norm”,
    sizemode=“absolute”, sizeref=10, # cone size
fig.show()
))

The DataFrame already has the magnitude in df["Force_Mag"], which is the column I would like to use for coloring and for setting the color bar. I have tried several approaches without success. Is there a way to achieve this with go.Cone to achieve this type of visualization?

Try to use something like:

color=df['force_mag']

But it is not compatible, I was hoping to add a colormap other than the default one Plotly uses in go.cone

Upvotes: 3

Views: 38

Answers (0)

Related Questions