Using plotly.graph_objects modules Scatter3D & scatter3d.Line I can't get the line colors to fall within the range of the define color scale.
I tried the following code, that plots points as markers & lines by appending the respective "go.Scatter3D" into a data-list that is given as data to "go.FigureWidget"
import numpy as np
import plotly.graph_objects as go
# define function
def get_3D_plot_stack(x,y,z,connections,elongation_values):
data_list = [] #initializing empty list
### Points, gathering data and appending to data_list
for i,(xi,yi,zi) in enumerate(zip(x,y,z)): # looping through each point_list
data_list.append(go.Scatter3d(x=[xi], y=[yi], z=[zi],
mode='markers',
marker=dict(color='black',size=2),
showlegend= False))
### Lines, gathering data and appending to data_list
for i,(conn,elongation_i) in enumerate(zip(connections,elongation_values)):
xi_conn = [x[conn[0]], x[conn[1]]]
yi_conn = [y[conn[0]], y[conn[1]]]
zi_conn = [z[conn[0]], z[conn[1]]]
data_list.append(go.Scatter3d(
x=xi_conn, y=yi_conn, z=zi_conn,
mode='lines',
line=go.scatter3d.Line(
width = 4,
color=elongation_i,
colorscale='Viridis',
showscale=True, #set to TRUE
),
showlegend=False
))
### Create figure
fig = go.FigureWidget(data=data_list)
fig.show()
return()
x,y,z = np.random.random_sample((3,10)) # random points
connections = np.array([[0,1],[9,2],[2,3],[5,7],[6,8],[2,8],[1,2],[4,5]]) # line connections
elongation_values = np.random.random_sample((len(connections))) # random colors
get_3D_plot_stack(x,y,z,connections,elongation_values)
An improvement is made, by plotting the colorbar values for all lines once. This doesn't resolve the issue however (and neither does an implementation of the colorbar using the older workaround found in this GitHub issue: https://github.com/plotly/plotly.py/issues/1085 )
import numpy as np
import plotly.graph_objects as go
def get_3D_plot_stack(x,y,z,connections,elongation_values):
data_list = [] #initializing an empty list
### Points, gathering data and appending to data_list
for i,(xi,yi,zi) in enumerate(zip(x,y,z)): # looping through each point_list
data_list.append(go.Scatter3d(x=[xi], y=[yi], z=[zi],
mode='markers',
marker=dict(color='black',size=2),
showlegend= False))
### Lines, gathering data and appending to data_list
x_conn, y_conn, z_conn = np.empty((len(connections),2)), np.empty((len(connections),2)), np.empty((len(connections),2))
for i,(conn,elongation_i) in enumerate(zip(connections,elongation_values)):
xi_conn = [x[conn[0]], x[conn[1]]]
yi_conn = [y[conn[0]], y[conn[1]]]
zi_conn = [z[conn[0]], z[conn[1]]]
# storing data
x_conn[i], y_conn[i], z_conn[i] = xi_conn, yi_conn, zi_conn
data_list.append(go.Scatter3d(
x=xi_conn, y=yi_conn, z=zi_conn,
mode='lines',
line=go.scatter3d.Line(
width = 4,
color=elongation_i,
colorscale='Viridis',
showscale=False, #set to FALSE
),
showlegend=False
))
## getting the colorbar once
line_trace_all = go.Scatter3d( x=x_conn, y=y_conn, z=z_conn,
mode='lines',
line=go.scatter3d.Line(
color=elongation_values,
colorscale='Viridis',
showscale=True),
showlegend=False)
data_list.append(line_trace_all)
### Create figure
fig = go.FigureWidget(data=data_list)
fig.show()
return()
x,y,z = np.random.random_sample((3,10))
connections = np.array([[0,1],[9,2],[2,3],[5,7],[6,8],[2,8],[1,2],[4,5]]) #random
elongation_values = np.random.random_sample((len(connections)))
get_3D_plot_stack(x,y,z,connections,elongation_values)
Figure shows how colorbar values are only plotted once, but the colors are still off..