Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly - How to set width to specific line?

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.

like image 651
Kaow Avatar asked Oct 11 '19 05:10

Kaow


People also ask

What is margin in Plotly?

The default values for the margins are 80 pixels on the left, right, and bottom and 100 pixels on the top.

Is Plotly customizable?

Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments. New to Plotly? Plotly is a free and open-source graphing library for Python.

How do you plot a line plot in express Plotly?

Plotly have express.line () – function to create a line plot. The line () – function takes two list’s as parameters to plot in X, Y axes OR You can name the data set and put the names of the columns on the X & Y axes. There’s an optional parameter called title, a title for the plot.

How to change line width of a graph in Matplotlib?

Line Width : The width of a line is known as line width. One can change the line width of a graph in matplotlib using a feature. Set the line width by using line-width feature ( lw can also be used as short form ). Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

How to use scatter plot in Plotly?

plotly.graph_objects module (typically imported as go) has scatter function. go.Scatter can be used both for plotting points (markers) or lines, depending on the value of the mode provided. Values of mode are as follows: lines+markers: For line plot with markers in it. markers: Plot with markers only, no lines.

How to change the default continuous color scale in Plotly Express?

By way of example, in the following figure, simply setting the template argument will automatically change the default continuous color scale, even though we have not specified color_continuous_scale directly. Plotly Express supports a simple default-configuration system via the plotly.express.defaults singleton object.


Video Answer


3 Answers

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

like image 64
vestland Avatar answered Oct 23 '22 06:10

vestland


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])
like image 3
Mykola Zotko Avatar answered Oct 23 '22 05:10

Mykola Zotko


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.

like image 1
Sanil Khurana Avatar answered Oct 23 '22 05:10

Sanil Khurana