Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to make a Plotly scatter have smooth lines connecting points?

Tags:

plotly

I want to use something like a Plotly Scatter to plot my data, but I want to make the lines smooth. The only place I could think to look is at the Mode parameter.

If I were intent on a smooth plot, would I have to inject data to smooth it out?

like image 495
Seth Avatar asked Nov 29 '22 23:11

Seth


2 Answers

You can use the 'smoothing' option within the trace object. This option takes a value between 0 and 1.3, and you have to be sure to set 'shape' to 'spline':

smoothTrace = {'type' : 'scatter', 'mode' : 'lines', 
'x' : [1,2,3,4,5], 'y' : [4, 6, 2, 7, 8], 'line': {'shape': 'spline', 'smoothing': 1.3}}

plotly.offline.iplot([smoothTrace])

I've found that the amount of smoothing that this option gives is negligible at best though. I've had more success with using the Savitzy-Golay Filter that comes in the SciPy library. You don't need to set the 'shape' or 'smoothing' options; the filter works on the values themselves:

evenSmootherTrace =  {'type' : 'scatter', 'mode' : 'lines', 
'x' : scipy.signal.savgol_filter([1,2,3,4,5], 51, 3), 
'y' : [4, 6, 2, 7, 8]}

plotly.offline.iplot([evenSmootherTrace])

Hope this helps!

like image 170
Rytchbass Avatar answered Dec 11 '22 04:12

Rytchbass


https://plotly.com/python/line-charts/

import plotly.graph_objects as go
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 3, 2, 3, 1])

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=y + 5, name="spline",
                    text=["tweak line smoothness<br>with 'smoothing' in line object"],
                    hoverinfo='text+name',
                    line_shape='spline'))
like image 34
Reison Avatar answered Dec 11 '22 02:12

Reison