7

I'm trying to draw a graph with plotly libraries and I want to set a specific width of each line.

This is my code.


x_link = [125, 257, None, 125, 787, None]
y_link = [383, 588, None, 383, 212, None]
z_link = [65, 85, None, 65, 526, None]

link_size = [3,6]

trace1= go.Scatter3d(
                x = x_link, 
                y = y_link,
                z = z_link,
                line=dict(
                    color='#0061ff',
                    width=link_size
                )
)

But it raises this error.

Invalid value of type 'builtins.list' received for the 'width' property of scatter3d.line Received value: [3, 6]

The 'width' property is a number and may be specified as: - An int or float in the interval [0, inf]

So, is there a way to set the specific width of each line?

Thank you.

vestland
  • 55,229
  • 37
  • 187
  • 305
Kaow
  • 483
  • 2
  • 9
  • 22

3 Answers3

11

You're going to have to add individual traces for your dataset to do this since line=dict(width) only takes one single argument and not an array of some sort. And that can be a lot of work for larger datasets. But If you change your dataset from lists to a list of lists, you can take even more datapoints into account. So with a little data manipulation you can turn this:

x_link = [125, 257, None, 125, 787, None]
y_link = [383, 588, None, 383, 212, None]
z_link = [65, 85, None, 65, 526, None]

into this:

x_link = [[125, 257, None], [125, 787, None]]
y_link = [[383, 588, None], [383, 212, None]]
z_link = [[65, 85, None], [65, 526, None]]

and run this code:

import plotly.graph_objects as go
from plotly.offline import init_notebook_mode, iplot

x_link = [[125, 257, None], [125, 787, None]]
y_link = [[383, 588, None], [383, 212, None]]
z_link = [[65, 85, None], [65, 526, None]]

# figure formatting
colors=['red', 'green']
link_size = [2,12]

# make multiple traces
traces={}
for i in range(0, len(x_link)):
    traces['trace_' + str(i)]=go.Scatter3d(x = x_link[i], 
                                           y = y_link[i],
                                           z = z_link[i],
                                           line=dict(
                                                color=colors[i],
                                                width=link_size[i]))
data=list(traces.values())

# build and plot figure
fig=go.Figure(data)
fig.show()

to get this plot:

enter image description here

vestland
  • 55,229
  • 37
  • 187
  • 305
  • @MykolaZotko Ha Ha! Didn't see your suggestion before I posted mine. Which by the way is solemnly based on OP's sample snippet and previous answers of my own:https://stackoverflow.com/questions/57975944/how-to-make-a-multiple-trace-plot-as-a-reusable-code/57987361#57987361 – vestland Oct 11 '19 at 10:30
  • @MykolaZotko But you have my upvote. UpvoteS to be exact. Took a look at your profile and found some other very useful contributions =) – vestland Oct 11 '19 at 10:31
  • Thank you! If you haven't seen my solution why do you import `init_notebook_mode`?;) You need to add `init_notebook_mode(connected=True)` if you want to use plotly in jupyter notebook. You also didn't use the function `iplot()`. – Mykola Zotko Oct 11 '19 at 10:36
  • 1
    I used it in the previous version of my solution. But it's ok :) – Mykola Zotko Oct 11 '19 at 11:49
3

You need to make multiple traces and specify width for each trace:

x_link = [125, 257, None, 125, 787, None]
y_link = [383, 588, None, 383, 212, None]
z_link = [65, 85, None, 65, 526, None]

link_size = [3,6]

trace1= go.Scatter3d(
                x = x_link[:3], 
                y = y_link[:3],
                z = z_link[:3],
                line=dict(
                    color='green',
                    width=link_size[0]
                )
)

trace2= go.Scatter3d(
                x = x_link[3:], 
                y = y_link[3:],
                z = z_link[3:],
                line=dict(
                    color='red',
                    width=link_size[1]
                )
)

fig = go.Figure(data=[trace1, trace2])
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
1

This is the documentation of what you are trying to do https://plot.ly/python/reference/#scatter-line

Line accepts only a single value for width, not an array of values, unlike you have given it.

Try passing in only 6 and it should not give an error.

Sanil Khurana
  • 1,129
  • 9
  • 20
  • @Sanil Khurana thank you for response, but if i want to set width of first line is 3 and second line is 6, how can i do that? – Kaow Oct 11 '19 at 06:48