Issue
I'm trying to draw a spline, or interpolated line, in plotly python, however, it doesn't seem possible with the usual spline option since the curve doubles back on itself. Is there a way to achieve a smooth curve in this instance? Are there other libraries that might do the job that I can call?
Edit: sample data
a = array([0.15, 0.15, 0.17, 0.2 , 0.21, 0.24, 0.26, 0.27, 0.27, 0.29, 0.32, 0.35, 0.39, 0.4 , 0.4 , 0.41, 0.47, 0.48, 0.51, 0.52, 0.54, 0.56, 0.59, 0.62, 0.63, 0.65, 0.69, 0.73, 0.74])
b = array([0.14, 0.15, 0.1 , 0.17, 0.17, 0.18, 0.05, 0.16, 0.17, 0.04, 0.03, 0.14, 0.13, 0.13, 0.14, 0.14, 0.13, 0.13, 0.14, 0.14, 0.15, 0.16, 0.18, 0.2 , 0.21, 0.22, 0.24, 0.25, 0.25])
c = array([0.71, 0.7 , 0.73, 0.63, 0.62, 0.58, 0.69, 0.57, 0.56, 0.67, 0.65, 0.51, 0.48, 0.47, 0.46, 0.45, 0.4 , 0.39, 0.35, 0.34, 0.31, 0.28, 0.23, 0.18, 0.16, 0.13, 0.07, 0.02, 0.01])
Example code...
fig5 = go.Figure(go.Scatterternary({
'mode': 'lines',
'connectgaps': True,
'a': a6,
'b': b6,
'c': c6,
'line': {'color': 'black', 'shape': 'spline', 'smoothing': 1},
'marker': {'size': 2, 'line': {'width': 0.1}}
})
)
fig5.add_trace(go.Scatterternary({
'mode': 'markers',
'a': a6,
'b': b6,
'c': c6,
'marker': {'size': 2},
'connectgaps': True,
})
)
fig5.show(renderer="svg")
Solution
You can use the geometry of the triangular coordinates to isolate portions of the curve you want to separate, then use a condition to select this portion of the data. For example, if we select the following portion of the curve:
This can be isolated with a rule like (b < 0.15) & (c > 0.6)
. To make this process a bit more general, I wrote a function that allows you to pass lists of indices you want to plot, and the scatter and spline are drawn for these groups separately. You can break the curve up into as many pieces as needed this way.
import numpy as np
import plotly.graph_objects as go
a = np.array([0.15, 0.15, 0.17, 0.2 , 0.21, 0.24, 0.26, 0.27, 0.27, 0.29, 0.32, 0.35, 0.39, 0.4 , 0.4 , 0.41, 0.47, 0.48, 0.51, 0.52, 0.54, 0.56, 0.59, 0.62, 0.63, 0.65, 0.69, 0.73, 0.74])
b = np.array([0.14, 0.15, 0.1 , 0.17, 0.17, 0.18, 0.05, 0.16, 0.17, 0.04, 0.03, 0.14, 0.13, 0.13, 0.14, 0.14, 0.13, 0.13, 0.14, 0.14, 0.15, 0.16, 0.18, 0.2 , 0.21, 0.22, 0.24, 0.25, 0.25])
c = np.array([0.71, 0.7 , 0.73, 0.63, 0.62, 0.58, 0.69, 0.57, 0.56, 0.67, 0.65, 0.51, 0.48, 0.47, 0.46, 0.45, 0.4 , 0.39, 0.35, 0.34, 0.31, 0.28, 0.23, 0.18, 0.16, 0.13, 0.07, 0.02, 0.01])
fig = go.Figure()
curve_portion = np.where((b < 0.15) & (c > 0.6))
curve_other_portion = np.where(~((b < 0.15) & (c > 0.6)))
def add_plot_spline_portions(fig, indices_groupings):
for indices in indices_groupings:
fig.add_trace(go.Scatterternary({
'mode': 'lines',
'connectgaps': True,
'a': a[indices],
'b': b[indices],
'c': c[indices],
'line': {'color': 'black', 'shape': 'spline', 'smoothing': 1},
'marker': {'size': 2, 'line': {'width': 0.1}}
})
)
fig.add_trace(go.Scatterternary({
'mode': 'markers',
'a': a[indices],
'b': b[indices],
'c': c[indices],
'marker': {'size': 10, 'color': 'red'},
'connectgaps': True,
})
)
add_plot_spline_portions(fig, [curve_portion, curve_other_portion])
fig.show()
Answered By - Derek O
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.