Change colour of curve according to its y-value in matplotlib

Hello, there is the possibility to color the y-values according to their value in matplotlib like

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(2*x)
plt.scatter(x,y,, edgecolor='none')

What is the corresponding way to do it in plotly?

import plotly.graph_objects as go
import numpy as np

N = 1000
t = np.linspace(0,10,100)
y = np.sin(t)

fig = go.Figure(
        data = [go.Scatter(x=t,y=y,mode = "lines")]

You can plot lines colored according to y-values, but not with scatter mode="lines", but "markers", and with a sufficiently high density of points. (the density is also influenced by layout width and height)

import plotly.graph_objects as go
import numpy as np

N = 700
t = np.linspace(0,2*np.pi,N) #N sufficiently big to ensure high marker density
y = np.sin(t)

fig = go.Figure(go.Scatter(x=t,y=y,mode = "markers", marker=dict(color=y, size=3,  
                           colorscale="matter_r", colorbar_thickness=25)))
fig.update_layout(width=600, height=400)


1 Like