# Python Plotly: Coloring go.Scatter lines according to separate array value

**URL:** <https://community.plotly.com/t/python-plotly-coloring-go-scatter-lines-according-to-separate-array-value/74440>\
**Category:** 📊 Plotly Python\
**Created:** [April 14, 2023, 8:48pm UTC](https://community.plotly.com/t/python-plotly-coloring-go-scatter-lines-according-to-separate-array-value/74440 "2023-04-14T20:48:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sean412](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@sean412](https://community.plotly.com/u/sean412)\
**Post date:** [April 14, 2023, 8:48pm UTC](https://community.plotly.com/t/python-plotly-coloring-go-scatter-lines-according-to-separate-array-value/74440/1 "2023-04-14T20:48:26Z")

</div>

Hi, I’m trying to plot multiple traces on one figure and color the lines according to a continuous color scale using plotly.graph\_objects. I’m having trouble getting them to color correctly. Example below:

* * *

`import plotly.graph_objects as go`  
`import numpy as np`

`x = np.array([[0,1,2,3,4,5],`  
` [0,1.1,2.1,3.1,4.1,5.1],`  
` [1,2,3,4,5,6],`  
` [2,3,4,5,6,7],`  
` [2,3.1,4.1,5.1,6.1,7.1],`  
` [3,4,5,6,7,8]])`  
`vals = [0,0,1,2,2,3]`  
`nums = [1,2,3,4,5]`  
`fig = go.Figure()`  
`for i, row in enumerate(x):`  
` fig.add_trace(go.Scatter(x=nums, y=row, name=vals[i], mode='lines',`  
` marker=dict(color=vals, cmax=3, cmin=0, colorscale='Viridis')))`  
`fig.show()`

* * *

I would like for the color of each trace to be determined by `vals`, so the first two traces would have the same color (purple on the Viridis scale?) since they have the same `vals[i]` value, the fourth and fifth traces would also have matching colors, and the sixth trace would be the max value of the color scale (yellow on the Viridis scale?)

Thanks!

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [April 14, 2023, 10:05pm UTC](https://community.plotly.com/t/python-plotly-coloring-go-scatter-lines-according-to-separate-array-value/74440/2 "2023-04-14T22:05:55Z")

</div>

HI @sean412 welcome to the forums.

If you change to `mode='lines+markers` you will see, that the markers are colored depending on their value. The thing is, that you want to color the lines which can be done via `line=dict(color=your_discrete_color)`  
You can specify only one color per line.

If you want to use the viridis colorscale for the line colors, you’ll hav to convert it to a discrete values, here an example:

> [@How to share a common color scale among different bar traces?](https://community.plotly.com/t/how-to-share-a-common-color-scale-among-different-bar-traces/71837/2):
>
> Hi @vedantroy welcome to the forums. I did not fully understand what you want to do. Instead of using dark24 you want to use viridis? The question is, how do you want to use it, here are three options (I added dark24 for clarity reasons): [newplot(37)] code: from plotly.express.colors import sample\_colorscale from sklearn.preprocessing import minmax\_scale import plotly.graph\_objects as go import itertools as it import numpy as np # create data x = np.ones(24) y = np.arange(0,24) cycled =…
