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.
The default values for the margins are 80 pixels on the left, right, and bottom and 100 pixels on the top.
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.
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.
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.
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.
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.
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:
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])
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With