jupyter-scatter icon indicating copy to clipboard operation
jupyter-scatter copied to clipboard

connect points by straight lines

Open abast opened this issue 1 year ago • 6 comments

The example in the docs connects lines by curved lines:

data = '''0	0.13	0.27	A	2
1	0.87	0.93	A	1
2	0.10	0.25	B	2
3	0.03	0.90	A	3
4	0.19	0.78	B	1'''
data = [d.split('	') for d in data.split('\n')]
df = pd.DataFrame(data).set_index(0)
df.columns = ['x','y','group','order']
df['x'] = df.x.astype(float)
df['y'] = df.y.astype(float)
df['order'] = df.order.astype(float)

scatter = Scatter(data = df, x = 'x', y = 'y', width = 320, height = 320)
scatter.connect(by='group')

scatter.show()
image

Would it be possible to connect points with straight lines instead? How exactly are the lines computed?

abast avatar May 30 '24 23:05 abast

Right now all lines are spline interpolated as that works well across many use cases. Unfortunately you can't control the "curviness" or even render straight lines. Technically that would of course be possible but we would need to implement that feature for regl-scatterplot first. I don't have the bandwidth for working on this at the moment but if you have time, please go ahead and take a stab at it. I don't think it would be a hard lift to enable straight lines as all we really need is to skip the spline interpolation step when creating the line segments.

flekschas avatar May 31 '24 12:05 flekschas

Can you point me to the code that deals with plotting the lines?

abast avatar Jun 03 '24 16:06 abast

It should be fairly straight forward. All we gotta do is have an option to skip createSplineCurve in https://github.com/flekschas/regl-scatterplot/blob/master/src/index.js#L2014 and instead pass newPoints directly into https://github.com/flekschas/regl-scatterplot/blob/master/src/index.js#L2020. The only extra bit we need is a function that groups newPoints such that each set of connected points is a flat list of 2D point coordinates. E.g., we need to separately call groupPoints() from https://github.com/flekschas/regl-scatterplot/blob/master/src/spline-curve-worker.js#L205-L231.

flekschas avatar Jun 04 '24 12:06 flekschas

I just realized, there's an easy workaround to achieve straight lines:

scatter.options({ 'pointConnectionTolerance': 1 })

By increasing the tolerance, fewer to literally no interpolated points are added. E.g:

With a tolerance of 1 / 5000

Screenshot 2024-06-04 at 9 41 01 PM

With a tolerance of 1

Screenshot 2024-06-04 at 9 40 27 PM

This isn't the very best solution but at least it should get you there without any code changes.

flekschas avatar Jun 05 '24 01:06 flekschas

That's great, thanks for this workaround and for having another look at it!

abast avatar Jun 05 '24 02:06 abast

FYI, there are a few glitches related to connected scatterplots I recently found and fixed in #125. I'm hoping to cut a new release with the fixes soon 🤞

flekschas avatar Jun 05 '24 18:06 flekschas